Start learning free

LESSON 65 OF 677 · STRINGS I

String Strip / Trim in Kotlin

Lesson

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.

STEP 1 OF 1
    Ctrl + Enter to check