Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: remove first and last character of string

by syphilis (Archbishop)
on Oct 14, 2020 at 02:26 UTC ( [id://11122802]=note: print w/replies, xml ) Need Help??


in reply to remove first and last character of string

i've thought of using substr but i can't be sure that the number above in quotes will always be the same number of characters

You can use substr() without knowing the number of characters.
To remove the first character of $str: substr($str,  0, 1, '')
To remove the last character of $str : substr($str, -1, 1, '')
Or remove the last character of $str : substr($str, length($str) - 1, '')
Or, just use chop() to remove the last character, as you've already noted.

Cheers,
Rob

Replies are listed 'Best First'.
Re^2: remove first and last character of string
by flieckster (Scribe) on Oct 14, 2020 at 02:44 UTC
    Thanks Rob, but is there a way use this Or remove the last character of $str : substr($str, length($str) - 1, '') to remove the last and first in one line?

      Win8 Strawberry 5.8.9.5 (32) Tue 10/13/2020 23:18:23 C:\@Work\Perl\monks >perl -Mstrict -Mwarnings my $s = '"654321_1111"'; print "'$s' \n"; $s = substr $s, 1, -1; print "'$s' \n"; ^Z '"654321_1111"' '654321_1111'


      Give a man a fish:  <%-{-{-{-<

        $s = substr $s, 1, -1;

        Nice.
        I didn't know that would work. I would have done it as:
        $s = substr $s, 1, length($s) - 2;
        But I've read the documentation, now ;-)

        Cheers,
        Rob

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11122802]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-04-23 13:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found