Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Answer: How can I strip an arbitary number of leading spaces from a string?

( #33922=categorized answer: print w/ replies, xml ) Need Help??

Q&A > strings > How can I strip an arbitary number of leading spaces from a string? contributed by chromatic

Use a regex with a quantifier. Use this one if you're not sure there will be any spaces at the start of the string:

$string =~ s/^\s*//;

Use this one if you know there will be at least one:

$string =~ s/^\s+//;

{QA Editors note: As japhy pointed out, the "+" quantifier is preferable over the "*" quantifier, even if it's unknown whether the string being tested has leading whitespace or not. With s/^\s+//;, strings lacking leading whitespace simply will be passed over. Furthermore, the "+" quantifier is faster than the "*" quantifier.}

Comment on Answer: How can I strip an arbitary number of leading spaces from a string?
Select or Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-09-20 12:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (159 votes), past polls