常用的 perl 函數中沒有trim, ltrim, rtrim這三個函數,那就自己來寫,非常容易
sub rtrim { my $str=shift; $str =~ s/\s+$//g; return $str; } sub ltrim { my $str=shift; $str =~ s/^\s+//g; return $str; } sub trim { my $str=shift; $str = &rtrim( $str); $str = <rim( $str); return $str; }
使用方法:
$str= " This is a spacing string ";
$str = &trim($str); // "This is a spacing string"
原文2009-11-23 10:04:58