Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: remove both leading and trailing spaces

by davido (Cardinal)
on Sep 26, 2003 at 06:18 UTC ( [id://294345]=note: print w/replies, xml ) Need Help??


in reply to remove both leading and trailing spaces

Welcome to the Monastery!

If you want to do it in one statement, you can do this:

my $val = " Smart Way "; $val =~ s/^\s+|\s+$//g; print '>', $val, '<';

You asked for a one-line solution. That will do it. A better solution (still using s///) is two lines, since alternation is expensive if you value time efficiency.

I feel somewhat guilty for having retyped an answer that has been provided right at our fingertips in the Q&A section under the heading, How do I remove whitespace at the beginning or end of my string?. If you look there you'll see a lot of different alternatives of which one is sure to be right for any occasion. ;) This being your first post I just want to encourage you to familiarize yourself with the more helpful aspects of the Monastery such as the Q&A section, and the Tutorials section. The Q&A section is a FAQ of sorts; not a place to post questions, but rather, a place to post answers to common questions. You'll find a wealth of information there, most good, some just ok, but all there for edification. It also is a great idea to become familiar with the perldocs. If you have Perl, you have the perldocs. If you prefer reading them online, you can find them right here in the Monastery as well. Enjoy!

Oh, this isn't related to your question, but rather, to the presentation of your question. I wanted to mention that you don't need to use <br> tags to create new lines within <code></ code> tags. Take a look at how your original post looks and you'll see what I mean. You did preview right?

Dave

"If I had my life to do over again, I'd be a plumber." -- Albert Einstein

Replies are listed 'Best First'.
Re: Re: remove both leading and trailing spaces
by s_mile (Acolyte) on Sep 26, 2003 at 09:46 UTC
    Hey Thanks to both of you, I presumed that one statement means less computing time,
    but your posts proved that's not always the case.
    Cool & Thanks for your inputs Davido, ..will go thru em.
    Blessings

    s_mile
      If you want to know why, then I recommend you get a copy of Jeffrey Friedl's book "Mastering Regular Expressions". It explains exactly how this sort of thing works and the how to develop regular expressions efficiently.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 14:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found