Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: what is the function should I need to use for trim white spaces ?

by davido (Cardinal)
on Jan 11, 2006 at 19:17 UTC ( #522534=note: print w/replies, xml ) Need Help??


in reply to what is the function should I need to use for trim white spaces ?

There's not a built-in. The regexp approach is fine. If you want it to be a sub, just put it in a sub definition:

sub trim_white { my $string = shift; $string =~ s/^\s+//g; $string =~ s/\s+$//g; return $string; } # Usage example: my $trimmed = trim_white( $untrimmed );

Update: For completeness's sake, it might be worth noting that on CPAN you can find String::Util, which includes a function called trim() that does exactly what you're talking about. ...it's been done. ;)


Dave

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2020-05-31 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (173 votes). Check out past polls.

    Notices?