Skip to content

Instantly share code, notes, and snippets.

@managai
Created October 1, 2011 09:31
Show Gist options
  • Save managai/1255802 to your computer and use it in GitHub Desktop.
Save managai/1255802 to your computer and use it in GitHub Desktop.
(perl) trim function to strip whitespace from a string
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
# Left trim function to remove leading whitespace
sub ltrim($)
{
my $string = shift;
$string =~ s/^\s+//;
return $string;
}
# Right trim function to remove trailing whitespace
sub rtrim($)
{
my $string = shift;
$string =~ s/\s+$//;
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment