To remove all whitespace characters from a string in Perl, you can use the s/\s+//g
substitution operator.
In this example,
$str
with a value of "Hello World"
.s/\s+//g
to remove all whitespace characters from $str
.$str
.$str = "Hello World";
$str =~ s/\s+//g;
print "Modified string: $str\n";
Modified string: HelloWorld
In this example,
$str
with a value of "apple\tbanana\n cherry "
.s/\s+//g
to remove all whitespace characters from $str
.$str
.$str = "apple\tbanana\n cherry ";
$str =~ s/\s+//g;
print "Modified string: $str\n";
Modified string: applebananacherry
In this tutorial, we learned How to Remove All Whitespace Characters from a String in Perl language with well detailed examples.