0
0
Kotlinprogramming~20 mins

String methods (substring, split, trim) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of substring and trim
What is the output of this Kotlin code?
Kotlin
val text = "  Kotlin Programming  "
val result = text.trim().substring(0, 6)
println(result)
A" Kot"
B" Kotlin"
C"Kotlin "
D"Kotlin"
Attempts:
2 left
💡 Hint
Remember trim() removes spaces at the start and end before substring is applied.
Predict Output
intermediate
2:00remaining
Splitting a string and accessing elements
What will be printed by this Kotlin code?
Kotlin
val data = "apple,banana, cherry, date"
val fruits = data.split(",")
println(fruits[2].trim())
A"banana"
B" cherry"
C"cherry"
D"date"
Attempts:
2 left
💡 Hint
Check how split works and remember to trim spaces after splitting.
🔧 Debug
advanced
2:00remaining
Identify the error in substring usage
What error will this Kotlin code produce?
Kotlin
val s = "Hello"
val sub = s.substring(3, 2)
println(sub)
AStringIndexOutOfBoundsException
BPrints "lo"
CCompilation error: wrong substring parameters
DNo output, prints empty string
Attempts:
2 left
💡 Hint
Check the order of start and end indices in substring.
Predict Output
advanced
2:00remaining
Count words after splitting and trimming
What is the output of this Kotlin code?
Kotlin
val sentence = "  Kotlin is fun  "
val words = sentence.trim().split(" ").filter { it.isNotEmpty() }
println(words.size)
A2
B3
C5
D4
Attempts:
2 left
💡 Hint
Trim removes spaces at ends, split by space, then filter removes empty strings.
🧠 Conceptual
expert
3:00remaining
Understanding substring and split behavior
Given the Kotlin code below, what will be the output?
Kotlin
val input = "one,two,,four,"
val parts = input.split(",")
val sub = parts[2].substring(0, 0)
println("${parts.size} ${sub.isEmpty()}")
A"5 true"
B"4 false"
C"5 false"
D"4 true"
Attempts:
2 left
💡 Hint
Check how split handles consecutive commas and trailing commas, and substring with same start and end index.