http://www.perlmonks.org?node_id=940206


in reply to How do I remove whitespace at the beginning or end of my string?

Remove spaces at the beginning and end of your string. The fastest way, so far.
my $l = length($string); $string = reverse unpack("A$l",reverse unpack("A$l",$string));


Rate two-s/// split m-capture unpack s-with-\G two-s/// 239349/s -- -6% -38% -40% -41% split 255782/s 7% -- -33% -36% -37% m-capture 384362/s 61% 50% -- -4% -5% unpack 399865/s 67% 56% 4% -- -1%

Replies are listed 'Best First'.
Re: Answer: How do I remove whitespace at the beginning or end of my string?
by jwkrahn (Abbot) on Nov 26, 2011 at 21:36 UTC

    No need for the string interpolation:

    $string = reverse unpack( 'A*', reverse unpack( 'A*', $string ) );

      It probably also depends on what perl you use, but I'm amazed by the unpack speed. Here's perl-5.14.2 bench:

      use Benchmark "cmpthese"; use Regexp::Common "whitespace"; my $f = " this is a string with spaces to remove "; cmpthese (-3, { "Regexp-Common" => sub { my $g = $f; $g =~ s/$RE{ws}{crop}//g; }, "two-s///" => sub { my $g = $f; $g =~ s/^\s+//; $g =~ s/\s+$/ +/; }, "one-s///" => sub { my $g = $f; $g =~ s/^\s+|\s+$//g; }, "s-capture" => sub { my $g = $f; $g =~ s/^\s*(.*?)\s*$/$1/; }, "s-capture2" => sub { my $g = $f; $g =~ s/^\s*(\S+(?:\s+\S+)*)? +\s*$/$1/; }, "m-capture" => sub { my $g = $f; ($g) = ($g =~ m/(\S+(?:\s+\S+ +)*)/); }, "unpack" => sub { my $g = $f; $g = reverse unpack "A*", rev +erse unpack "A*", $g; }, });

      And the runs ...

      5.15.5: Rate one-s/// s-capture s-capture2 m-capture two-s/// + unpack one-s/// 146152/s -- -30% -50% -67% -78% + -88% s-capture 208100/s 42% -- -30% -53% -69% + -83% s-capture2 295196/s 102% 42% -- -34% -56% + -76% m-capture 446788/s 206% 115% 51% -- -34% + -64% two-s/// 673459/s 361% 224% 128% 51% -- + -46% unpack 1239336/s 748% 496% 320% 177% 84% + -- 5.14.2: Rate one-s/// s-capture s-capture2 m-capture two-s/// + unpack one-s/// 137451/s -- -28% -55% -67% -80% + -88% s-capture 191603/s 39% -- -38% -53% -72% + -84% s-capture2 306586/s 123% 60% -- -25% -56% + -74% m-capture 410529/s 199% 114% 34% -- -41% + -65% two-s/// 692258/s 404% 261% 126% 69% -- + -41% unpack 1172943/s 753% 512% 283% 186% 69% + -- 5.14.1 (production perl with Rexexp::Common): Rate Regexp-Common one-s/// s-capture s-capture2 m- +capture two-s/// unpack Regexp-Common 35655/s -- -79% -87% -91% + -94% -96% -97% one-s/// 171398/s 381% -- -36% -54% + -69% -79% -88% s-capture 268800/s 654% 57% -- -29% + -52% -67% -81% s-capture2 376317/s 955% 120% 40% -- + -32% -53% -74% m-capture 556706/s 1461% 225% 107% 48% + -- -31% -61% two-s/// 808968/s 2169% 372% 201% 115% + 45% -- -43% unpack 1424354/s 3895% 731% 430% 278% + 156% 76% -- 5.12.4: Rate one-s/// s-capture s-capture2 m-capture two-s/// + unpack one-s/// 136992/s -- -32% -52% -69% -80% + -88% s-capture 200658/s 46% -- -30% -54% -71% + -82% s-capture2 288356/s 110% 44% -- -34% -58% + -75% m-capture 437898/s 220% 118% 52% -- -37% + -62% two-s/// 694505/s 407% 246% 141% 59% -- + -39% unpack 1143892/s 735% 470% 297% 161% 65% + -- 5.10.1: Rate one-s/// s-capture s-capture2 m-capture two-s/// + unpack one-s/// 154239/s -- -31% -53% -69% -79% + -88% s-capture 221974/s 44% -- -32% -55% -70% + -83% s-capture2 325661/s 111% 47% -- -34% -56% + -75% m-capture 490550/s 218% 121% 51% -- -34% + -62% two-s/// 740666/s 380% 234% 127% 51% -- + -42% unpack 1278065/s 729% 476% 292% 161% 73% + -- 5.8.9: Rate s-capture one-s/// s-capture2 m-capture two-s/// + unpack s-capture 230743/s -- -12% -29% -50% -76% + -83% one-s/// 262166/s 14% -- -19% -43% -73% + -81% s-capture2 323587/s 40% 23% -- -30% -67% + -76% m-capture 463883/s 101% 77% 43% -- -53% + -66% two-s/// 977938/s 324% 273% 202% 111% -- + -28% unpack 1354222/s 487% 417% 319% 192% 38% + -- 5.6.2: Rate s-capture one-s/// s-capture2 m-capture two-s/// + unpack s-capture 331911/s -- -11% -28% -50% -78% + -79% one-s/// 372325/s 12% -- -19% -44% -75% + -76% s-capture2 462448/s 39% 24% -- -30% -69% + -71% m-capture 664093/s 100% 78% 44% -- -56% + -58% two-s/// 1503714/s 353% 304% 225% 126% -- + -5% unpack 1574832/s 374% 323% 241% 137% 5% + --

      Enjoy, Have FUN! H.Merijn

        Yup, unpack is packed chock full of speedy goodness, but as you probably know, it only handles " \r\f\t\n", won't handle the unicode whitespace like

        #~ my $f = " \x{000B} vertical tab not removed \x{000B} "; my $f = " \x{00A0} no-break space not removed \x{00A0} ";