The trim() Function is used to remove any leading and trailing whitespace from the String.
fun main() {
var x = " Hello "
var y = x.trim()
println(y)
}
In the above code, we have declared a String ' Hello ' with leading and trailing spaces.
-Function1.png)
Then we have used the 'trim()' function to remove any unwanted spaces from beginning and end.
-Function2.png)
Unwanted spaces are stripped out of it.