Trimming a string is the method of removing extra spaces from the string. It can be left removal, right removal, or removal of all spaces from the string.
To remove blank spaces from a string, Scala provides a trim() method. The trim() will remove spaces from both sides, i.e. before the characters(leading) and from the end(trailing).
Syntax:
string.trim()
The method doesn't accept any parameter and returns a string with spaces removed from it.
Program to remove spaces string using trim()
Output
Trimming string from left or right
We can optionally trim a string is scala from the left (removing leading spaces) called left-trim and from the right (removing trailing spaces) called right-trim.
This is done using the replaceAll() methods with regex. We will find all the space (right/left) using the regular expression and then replace them with "".
Program to remove left and right spaces
Output
need an explanation for this answer? contact us directly to get an explanation for this answer