trim - removing leading and trailing white spaces with Perl - strip whitespace perl

Category

removing the white spaces strip whitespace perl


This can be done using trim function in Perl. Trim function uses the regular expression to remove white spaces. It is not a library function but defined by the user.

In some other languages there are functions called ltrim and rtrim to remove spaces and tabs from the beginning and from the end of a string respectively.

Here's one approach using a regular expression: $string =~ s/^\s+|\s+$//g ; # remove both leading and trailing whitespace. Perl 6 will include a.

Use the subroutine below to trim whitespace (spaces and tabs) from the beginning and end of a string in Perl. This function is directly based on.