Is there a built-in function to trim leading and trailing whitespace such that trim(" hello world ") eq "hello world"
?

- 76,451
- 45
- 104
- 130
-
4FYI: string equality in Perl is tested by the operator `eq`. – A. Rex Jan 04 '11 at 20:16
-
6A bit of clarification on all the asnwers you got: `s/^\s+|\s+$//g;` vs `s/^\s*//; s/\s*$//;` The latter is the (ever so slightly) more idiomatic way to do this, as starting the regex engine over is actually faster than the alternation, in this case. You can read more about this on Jeffrey Friedl's Mastering Regular Expressions. (Unless this was fixed in some newer version of Perl, in which case someone please correct me!) – Hugmeir Jan 04 '11 at 20:30
-
5Coming from a Java and .NET background, I'm almost shocked this isn't built into the language! THANKS ALL! – Landon Kuhn Jan 04 '11 at 20:50
-
4@landon9720, it somewhat is: Scalar::Util has trim, and is core since 5.7.3 - That's 2002! – Hugmeir Jan 04 '11 at 20:52
-
4Hugmeir, this is wrong, [see the answer of Ether](http://stackoverflow.com/q/4597937#4598148). – daxim Jan 05 '11 at 07:27
-
1Comparison of even more functions: http://www.illusori.co.uk/perl/2010/03/05/advanced_benchmark_analysis_1.html http://blog.stevenlevithan.com/archives/faster-trim-javascript – daxim Jan 05 '11 at 07:40
10 Answers
Here's one approach using a regular expression:
$string =~ s/^\s+|\s+$//g ; # remove both leading and trailing whitespace
Perl 6 will include a trim function:
$string .= trim;
Source: Wikipedia

- 811,555
- 193
- 1,581
- 1,452
This is available in String::Util with the trim
method:
Editor's note: String::Util
is not a core module, but you can install it from CPAN with [sudo] cpan String::Util
.
use String::Util 'trim';
my $str = " hello ";
$str = trim($str);
print "string is now: '$str'\n";
prints:
string is now 'hello'
However it is easy enough to do yourself:
$str =~ s/^\s+//;
$str =~ s/\s+$//;
-
1@mklement0 nor will it ever be. But this is not relevant, since everyone should be using modules from the CPAN. – Ether Jun 09 '15 at 21:12
-
3why should everyone use modules from CPAN? It makes consistency difficult when using perl from your linux distribution (debian, redhat, ubuntu) combined with manually installed CPAN modules. It is much better if something can be done in perl using modules which are available as packages in linux distribution – Marki555 Jan 11 '16 at 10:01
-
1@Marki555 modules available as packages in your linux distro *are* from CPAN -- they've just been repackaged. You can generally request a certain module to be packaged if it hasn't been done so already (the debian folks are particularly responsive and helpful). – Ether Jan 12 '16 at 18:57
-
3I know they are also from CPAN... Yes, generally I can request a new pkg for debian, but it won't help me for my installed debian stable release... that's why I prefer packages modules, but use directly CPAN if really needed. – Marki555 Jan 13 '16 at 08:20
-
5@Ether With all due respect, I really appreciate knowing that this is a non-core module. This post is talking about using a module in lieu of a fairly simple regex one-liner. If the module is core, I would be much more open to it. It is relevant in this case. – UncleCarl Mar 01 '18 at 16:57
-
1I would prefer the possessive \s++ over \s+ It is slightly more efficient, because it does not use backtracking. Although admittedly, this only matters if you invoke it an insane amount of times. – Erik Lievaart Oct 29 '19 at 12:58
There's no built-in trim
function, but you can easily implement your own using a simple substitution:
sub trim {
(my $s = $_[0]) =~ s/^\s+|\s+$//g;
return $s;
}
or using non-destructive substitution in Perl 5.14 and later:
sub trim {
return $_[0] =~ s/^\s+|\s+$//rg;
}

- 23,572
- 15
- 99
- 156

- 142,882
- 41
- 325
- 378
Complete howto in the perfaq here: http://learn.perl.org/faq/perlfaq4.html#How-do-I-strip-blank-space-from-the-beginning-end-of-a-string-

- 34,290
- 15
- 75
- 125

- 64,065
- 16
- 119
- 163
For those that are using Text::CSV I found this thread and then noticed within the CSV module that you could strip it out via switch:
$csv = Text::CSV->new({allow_whitespace => 1});
The logic is backwards in that if you want to strip then you set to 1. Go figure. Hope this helps anyone.

- 379
- 4
- 16
I also use a positive lookahead to trim repeating spaces inside the text:
s/^\s+|\s(?=\s)|\s+$//g

- 91
- 6
Apply: s/^\s*//; s/\s+$//;
to it. Or use s/^\s+|\s+$//g
if you want to be fancy.

- 142,882
- 41
- 325
- 378

- 7,566
- 2
- 35
- 40
No, but you can use the s///
substitution operator and the \s
whitespace assertion to get the same result.

- 8,975
- 2
- 37
- 47
-
That would remove spaces between words, not just at either end of the string. – DarenW May 01 '12 at 18:06
-