Friday, February 15, 2008

Perl: Trim a string


Share at Facebook

There are many ways to trim a string in perl. Below is one of them. I used a simple regular expression that just removes the whitespaces from begin and end of the string.


## Trim function.
sub trim{
   my $string = shift;
   $string =~ s/^\s+|\s+$//g;
   return $string;
}
print "[".trim(" My name is Deadman ")."]\n";