Removing unwanted spaces from a string can be used to store only the required data and to remove the unnecessary trailing spaces. 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 whenever required. The types of trim functions are:
- Left Trim (~ s/^\s+//): Removes extra spaces from leftmost side of the string till the actual text starts. From the leftmost side the string takes 1 or more white spaces (\s+) and replaces it with nothing.
- Right Trim (~ s/\s+$//): Removes extra spaces from rightmost side of the string till the actual text end is reached. From the rightmost side the string takes 1 or more white spaces (\s+) and replaces it with nothing.
- Trim (~ s/^\s+|\s+$//): It removes extra space from both sides of the string.
Example: The following code demonstrates the left trim, right trim and trim in Perl:
The Original String is: Geeks---- After Lefttrim Str1: Geeks---- After Righttrim Str1: ----Geeks After trim Str1: Geeks
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Perl vs C/C++
- Perl | my keyword
- Perl | chr() Function
- Perl | hex Function
- Perl | exp Function
- Perl | Variables
- Perl | Classes in OOP
- Recursion in Perl
- Perl | lt operator
- Perl | eq operator
- Perl | ge operator
- Perl | uc() Function
- Perl | tr Operator
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.