The trim()
method is used to remove all the leading and trailing spaces that are present in the string. For example, if the string looks like, " Hello World "
then there is a lot of empty space before the term “Hello” and after the term “World”.
The method trim() is used to remove these spaces.
Title | Trim |
" Hello World " | "Hello World" |
" Lord of the rings " | "Lord of the rings" |
" Fight club " | "Fight club" |
string.trim()
Using this method returns a string without any trailing of leading white spaces.
Let’s understand it better with a coding example.
object Main extends App {// apply trim on a stringval data = " Hello World ".trim()// Display resultant dataprintln(data)val data1 = "Lord of the rings ".trim()// Display resultant dataprintln(data1)}