|Don't ask to ask, just ask|
Pulling white space off before/after string?by ecuguru (Monk)
|on Apr 17, 2007 at 04:26 UTC||Need Help??|
ecuguru has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to clean up some web page filtering, and I've got strings that have too much white space. like:
" New Hampshire "
"bob tom dick harry "
Strings that should have spaces inside them, but with spaces at the beginning and end that I need to get rid of. I'm guessing that I should make a while loop that starts from the front and if the character is a space, keep going, until I get to a non-space character, and copy the remainder of the string. Repeat from the end, backwards.
But I'm figuring there has to be a better way to do this. How can I trim whitespace off of both ends (as small as one space, or as large as 30), without killing the let spaces inbetween characters??