Perl: function to trim string leading and trailing whitespace

后端 未结 10 1860
深忆病人
深忆病人 2021-02-01 00:12

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

10条回答
  •  孤城傲影
    2021-02-01 00:49

    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

提交回复
热议问题