Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Answer: How do I remove whitespace at the beginning or end of my string?

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

Q&A > regular expressions > How do I remove whitespace at the beginning or end of my string? contributed by Bobinours

I read somewhere (probably in the CookBook) that it's faster to execute this :
$string=~s/^\s+//; $string=~s/\s+$//;
Than the one-liner :

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

Edited by davido: Removed useless use of /g modifier from the two-line (faster) example.

Comment on Answer: How do I remove whitespace at the beginning or end of my string?
Select or Download Code
Re: Answer: How do I remove whitespace at the beginning or end of my string?
by Abigail-II (Bishop) on Jul 14, 2003 at 15:08 UTC
    Note that the /g in the two line example are pointless. There's no need to repeatedly remove all whitespace at the front or end.

    Abigail

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 pondering the Monastery: (4)
As of 2014-10-25 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (142 votes), past polls