In Kotlin, you can remove leading and trailing whitespace from a string using trim():
s.trim() // removes leading and trailing whitespace
s.trimStart() // removes only leading whitespace
s.trimEnd() // removes only trailing whitespace
Here are some examples:
val s = " hello "
println(s.trim()) // "hello"
println(s.trimStart()) // "hello "
println(s.trimEnd()) // " hello"
These methods return a new String — the original is not modified.
Assignment
Follow the Coding Tutorial and strip whitespace from s!
Hint
Use s.trim() and assign the result to result.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
