How do I remove white space in a Perl string?

后端 未结 12 1898
离开以前
离开以前 2020-12-31 09:34

If I declared a variable $myString with the value \'3 \' (notice the white space). Is there any function to remove the white space for the return v

12条回答
  •  失恋的感觉
    2020-12-31 09:53

    Here's a subroutine that will allow you to remove leading and trailing whitespace from a string while also removing excesss whitespaces from within the string and replacing it with a single spaces.

    -- routine

    sub unspace { my @stringer = @_ ? @_ : $; $ = join( ' ', split(' ')) for @stringer; return wantarray ? @stringer : "@stringer"; }

    -- usage

    $MySpacedString = ' String with tabs double-spaces and other whitespace areas. '; $MyCleanString = unspace($MySpacedString);

提交回复
热议问题