Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re: remove both leading and trailing spaces by davido
in thread remove both leading and trailing spaces by s_mile

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-19 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found