1

Is there an Java like trim function for Perl.

I am looking for function in Perl that removes all the leading and trailing characters below 0x20, like in Java.

After calling the function on the following string.

my $string = "\N{U+0020}\N{U+001f}\N{U+001e}\N{U+001d}\N{U+001c}\N{U+001b}\N{U+001a}\N{U+0019}\N{U+0018}\N{U+0017}\N{U+0016}\N{U+0015}\N{U+0014}\N{U+0013}\N{U+0012}\N{U+0011}Hello Moto\N{U+0010}\N{U+000f}\N{U+000e}\N{U+000d}\N{U+000c}\N{U+000b}\N{U+000a}\N{U+0009}\N{U+0008}\N{U+0007}\N{U+0006}\N{U+0005}\N{U+0004}\N{U+0003}\N{U+0002}\N{U+0001}\N{U+0000}";

Only "Hello Moto" should be left.

The trim from String::Util only removes the first whitespace (\N{U+0020}).

Birdy
  • 79
  • 5
  • 2
    `s/[\x00-\x1F]//g` removes all character below 0x20, but that's not what Java's String.trim does – ikegami May 12 '15 at 17:01
  • Ok, i forgot to write that it removes only the leading and trailing characters in the range of \x00-\x1F, right? Thanks – Birdy May 18 '15 at 11:05
  • Does this answer your question? [Perl: function to trim string leading and trailing whitespace](https://stackoverflow.com/questions/4597937/perl-function-to-trim-string-leading-and-trailing-whitespace) – qwerty Jun 13 '21 at 03:38

1 Answers1

6

The traditional ASCII way was to use

$string =~ s/^\s+|\s+$//g;

(i.e. remove whitespace (\s) from the beginning (^) and end ($) of the string.

U+001f is not whitespace, it's a Control. You can use Unicode properties in regular expressions with \p:

my $drop = qr/[\p{Space}\p{Cc}]+/;
$whitespace =~ s/^$drop|$drop$//g;

Or, more verbose:

$drop = qr/[\p{White_Space}\p{Cntrl}]+/;

You should probably change the name of the variable.

choroba
  • 231,213
  • 25
  • 204
  • 289