0
0
Kotlinprogramming~10 mins

String methods (substring, split, trim) in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get the substring "Hello" from the string.

Kotlin
val text = "Hello, World!"
val sub = text.[1](0, 5)
println(sub)
Drag options to blanks, or click blank then click option'
Asubstring
Bsplit
Ctrim
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using split instead of substring will not extract a substring by index.
Using trim removes spaces but does not extract parts of the string.
2fill in blank
medium

Complete the code to split the string into words using space as separator.

Kotlin
val sentence = "Kotlin is fun"
val words = sentence.[1](" ")
println(words.joinToString(", "))
Drag options to blanks, or click blank then click option'
Atrim
Bsplit
Csubstring
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using substring will not split the string into parts.
Using trim only removes spaces at the ends, not inside the string.
3fill in blank
hard

Fix the error in the code to remove spaces from the start and end of the string.

Kotlin
val raw = "  Kotlin  "
val clean = raw.[1]()
println("'$clean'")
Drag options to blanks, or click blank then click option'
Atrim
Bsplit
Csubstring
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using split will not remove spaces but split the string.
Using substring requires indexes and does not remove spaces automatically.
4fill in blank
hard

Fill both blanks to get the last word from the sentence.

Kotlin
val sentence = "Learn Kotlin easily"
val words = sentence.[1](" ")
val lastWord = words[[2]]
println(lastWord)
Drag options to blanks, or click blank then click option'
Asplit
Btrim
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using trim instead of split will not create a list of words.
Using index 0 will give the first word, not the last.
5fill in blank
hard

Fill all three blanks to extract "Kotlin" from the string with spaces.

Kotlin
val raw = "  Learn Kotlin  "
val clean = raw.[1]()
val words = clean.[2](" ")
val word = words[[3]]
println(word)
Drag options to blanks, or click blank then click option'
Atrim
Bsplit
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Not trimming first leaves spaces in the words.
Using index 0 selects "Learn", not "Kotlin".