Perl removes leading and trailing whitespace (removes left and right spaces, white space characters)

Time:2021-11-22

Othersprograming languageIn, there are functions ltrim and rtrim to remove spaces and tabs from the beginning and end of a string, respectively. Some also provide the function trim to delete white space characters at both ends of the string. These functions are not available in Perl because simple regular expression replacement can achieve this purpose (but I’m sure many cpan modules implement these functions). In fact, this is so simple that it has become a prominent theme in Parkinson’s trivial theorem.

Left side finishing

Ltrim or lstrip removes white space characters from the left side of the string:

Copy codeThe code is as follows:

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


Match one or more white space characters (\ S +) from the beginning of the string ^ and replace them with empty characters.

 

Right side finishing

Rtrim or rstrip remove white space characters from the right side of the string:

Copy codeThe code is as follows:

$str =~ s/\s+$//;


Match one or more white space characters (\ S +) up to the end of the string ($) and replace them with empty characters.

 

Finishing both ends

Trim removes white space characters at both ends of the string:

Copy codeThe code is as follows:

$str =~ s/^\s+|\s+$//g


Connect the above two regular expressions with the or notation |, and add / g at the end to perform the replacement operation globally (repeatedly).

 

Encapsulated in function

If you don’t want to see these structures in the code, you can add these functions to the code:

Copy codeThe code is as follows:

sub ltrim { my $s = shift; $s =~ s/^\s+//;       return $s };
sub rtrim { my $s = shift; $s =~ s/\s+$//;       return $s };
sub  trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };


Use it like this:

Copy codeThe code is as follows:

my $z = ” abc “;
printf “<%s>\n”, trim($z);   # <abc>
printf “<%s>\n”, ltrim($z);  # <abc >
printf “<%s>\n”, rtrim($z);  # < abc>
String::Util

 

String::Util

If you really don’t want to copy those things, you can install a module.

For example, string:: util provides the function trim, which you can use as follows:

Copy codeThe code is as follows:


use String::Util qw(trim);

 

my $z = ” abc “;
printf “<%s>\n”, trim( $z );              # <abc>
printf “<%s>\n”, trim( $z, right => 0 );  # <abc >
printf “<%s>\n”, trim( $z, left  => 0 );   # < abc>


By default, it arranges both sides. You don’t need to provide parameters. I think it will be clearer to implement ltrim and rtrim by myself.

 

Text::Trim

Another module, text:: trim, provides three functions, but it extremely adopts Perl style, which may be dangerous.

If you call it and use the return value in the print statement or assign it to a variable, it will return the sorted string and keep the original string unchanged.

Copy codeThe code is as follows:


use Text::Trim qw(trim);

 

my $z = ” abc “;
printf “<%s>\n”, trim($z);  # <abc>
printf “<%s>\n”, $z;       # < abc >


On the other hand, if you call it in a blank context, that is, without using the return value, the trim function will modify the parameters and produce a behavior similar to chomp.

Copy codeThe code is as follows:


use Text::Trim qw(trim);

 

my $z = ” abc “;
trim $z;
printf “<%s>\n”, $z;       # <abc>