Start learning free

LESSON 64 OF 348 · STRINGS I

String Strip / Trim in Scala in Scala

Lesson

In Scala, you can remove leading and trailing whitespace from a string using strip() (Scala 11+) or trim():

s.strip()        // removes leading and trailing whitespace (Unicode-aware)
s.trim()         // removes leading and trailing ASCII whitespace
s.stripLeading() // removes only leading whitespace
s.stripTrailing()// removes only trailing whitespace

Here are some examples:

var s = "  hello  "
println(s.strip())         // "hello"
println(s.stripLeading())  // "hello  "
println(s.stripTrailing()) // "  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.strip() and assign the result to result.

STEP 1 OF 1
    Ctrl + Enter to check

    String Strip in Scala

    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free