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.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
