Tuesday, May 08, 2007

Remove leading and trailing white space from a string

This is a routine task best done with two regexp's when using perl.
my $string = ' Mary had a little lamb. ';
$string =~ s/^\\s+//; #remove leading spaces
$string =~ s/\\s+$//; #remove trailing spaces
print $string;

1 comments:

Unknown said...

Needs a slight correction:

$string =~ s/^\s+//;
$string =~ s/\s+$//;

 
Blogger Template Layout Design by [ METAMUSE ] : Code Name Gadget 1.1 Power By freecode-frecode.blogger.com & blogger.com Programming Blogs - BlogCatalog Blog Directory