Start learning free

LESSON 64 OF 677 · STRINGS I

String Split in Kotlin

Lesson

In Kotlin, you can split a string into a list of substrings using split(). A string delimiter is matched as literal text:

s.split(delimiter)   // splits using a delimiter

Here are some examples:

val sentence = "hello world foo"
val words = sentence.split(" ")
// words == ["hello", "world", "foo"]

val csv = "a,b,c"
val parts = csv.split(",")
// parts == ["a", "b", "c"]

You can loop through the resulting list:

for (w in words) {
    println(w)
}

Assignment
Follow the Coding Tutorial and split sentence into a list of strings!


Hint
Use sentence.split(" ") and assign the result to words.

STEP 1 OF 1
    Ctrl + Enter to check