0
0
Kotlinprogramming~10 mins

String methods (substring, split, trim) in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String methods (substring, split, trim)
Start with original string
Apply substring(start, end)
Extract substring
Apply split(delimiter)
Create list of substrings
Apply trim() on each substring
Get cleaned substrings list
Use or print results
We start with a string, extract parts using substring, split it into pieces by a delimiter, then trim spaces from each piece.
Execution Sample
Kotlin
val text = "  Hello, Kotlin World!  "
val part = text.substring(2, 7)
val words = text.split(',')
val trimmedWords = words.map { it.trim() }
println(part)
println(trimmedWords)
This code extracts a substring, splits the string by comma, trims spaces from each part, and prints results.
Execution Table
StepActionInputOutputNotes
1Original string" Hello, Kotlin World! "" Hello, Kotlin World! "String with spaces at start and end
2substring(2,7)" Hello, Kotlin World! ""Hello"Extract characters from index 2 to 6
3split(',')" Hello, Kotlin World! "[" Hello", " Kotlin World! "]Split string into list by comma
4trim() each element[" Hello", " Kotlin World! "]["Hello", "Kotlin World!"]Remove spaces from start/end of each substring
5Print substring"Hello"HelloOutput substring
6Print trimmed list["Hello", "Kotlin World!"][Hello, Kotlin World!]Output trimmed list
7End--All operations done
💡 All string methods applied and results printed
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
text" Hello, Kotlin World! "" Hello, Kotlin World! "" Hello, Kotlin World! "" Hello, Kotlin World! "" Hello, Kotlin World! "
partundefined"Hello""Hello""Hello""Hello"
wordsundefinedundefined[" Hello", " Kotlin World! "][" Hello", " Kotlin World! "][" Hello", " Kotlin World! "]
trimmedWordsundefinedundefinedundefined["Hello", "Kotlin World!"]["Hello", "Kotlin World!"]
Key Moments - 3 Insights
Why does substring(2,7) return "Hello" and not include the character at index 7?
substring(start, end) includes characters from start index up to but not including end index, so index 7 is excluded as shown in execution_table step 2.
After splitting by comma, why do the substrings still have spaces?
split() only divides the string at the delimiter but does not remove spaces; trimming is needed separately as shown in step 4.
What does trim() do to each substring?
trim() removes spaces from the start and end of each substring, cleaning them up as seen in step 4 of execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'part' after step 2?
A" Hello"
B"Hello"
C"Hello,"
D"Hello, Kotlin"
💡 Hint
Check the output column in execution_table row with Step 2.
At which step does the list change from having spaces to trimmed strings?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the output column in execution_table rows 3 and 4 to see the difference.
If we change substring(2,7) to substring(0,5), what would be the new value of 'part'?
A"Hello"
B" He"
C" Hel"
D"Hello,"
💡 Hint
Remember substring includes characters from start index up to but not including end index.
Concept Snapshot
String methods in Kotlin:
- substring(start, end): extracts part from start index up to (not including) end index
- split(delimiter): splits string into list by delimiter
- trim(): removes spaces from start and end of string
Use these to extract, divide, and clean strings easily.
Full Transcript
We start with a string that has spaces around it. Using substring(2,7), we extract characters from index 2 up to 6, which gives "Hello". Then we split the original string by comma, creating a list with two parts, but these parts still have spaces. We apply trim() to each part to remove spaces at the start and end. Finally, we print the substring and the cleaned list. This shows how substring, split, and trim work step-by-step.