Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: howto strip the first char of a string?

by thospel (Hermit)
on Sep 08, 2004 at 13:26 UTC ( [id://389348]=note: print w/replies, xml ) Need Help??


in reply to howto strip the first char of a string?

In perl there are rarely recommended ways. Do what makes sense for whatever your exact situation is. The main issue will probably be if you want to change an existing string inplace or make a new string. And your ideas are fine for that.

New string:
$new = substr $old, 1

Modify old string:
substr($str, 0, 1) = ""
or
substr $str, 0, 1, ""
or
 $str =~ s/^.//s
(notice the s modifier, which is needed if you want the code to also work if the first character is a \n. You may want to replace the ^ with \A for extra paranoia)

The other main point to consider is what you want to happen if the original string is empty. Error ? Warning ? Silence and just give the empty string as result ? All solutions are easily modified for whatever you want there.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2024-04-18 23:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found