http://www.perlmonks.org?node_id=2259


in reply to How do I remove whitespace at the beginning or end of my string?

Use substitution along with the special characters ^ and $ which match the beginning and end of strings respectively along with \s which matches whitespace;
$string=~s/^\s+//; $string=~s/\s+$//;

Edited by davido: Removed useless use of /g modifier.
  • Comment on Re: How do I remove whitespace at the beginning or end of my string?
  • Download Code