0
0
Kotlinprogramming~10 mins

Multiline strings with trimIndent in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiline strings with trimIndent
Start multiline string with triple quotes
Write string lines with indentation
Call trimIndent()
Remove common leading whitespace
Return trimmed string
Use or print the string
The flow shows how Kotlin reads a multiline string, removes common indentation with trimIndent(), and returns the cleaned string.
Execution Sample
Kotlin
val text = """
    Hello,
      Kotlin!
""".trimIndent()
println(text)
This code creates a multiline string with indentation, removes common leading spaces using trimIndent(), and prints the cleaned string.
Execution Table
StepActionString ContentIndentation RemovedResulting String
1Start multiline string" Hello,\n Kotlin!"N/A" Hello,\n Kotlin!"
2Apply trimIndent()Same as above4 spaces (common indent)"Hello,\n Kotlin!"
3Print stringN/AN/AHello, Kotlin!
💡 All common leading spaces removed, string printed without extra indentation.
Variable Tracker
VariableStartAfter trimIndent()Final
text" Hello,\n Kotlin!""Hello,\n Kotlin!""Hello,\n Kotlin!"
Key Moments - 2 Insights
Why does trimIndent() remove only 4 spaces and not all spaces from each line?
trimIndent() removes the smallest common indentation from all lines, so it only removes 4 spaces because that is the minimum indentation shared by all lines (see execution_table step 2).
What happens to lines that have more indentation than the common minimum?
Lines with more indentation keep their extra spaces after trimIndent(), preserving relative indentation (see ' Kotlin!' line in execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 2, how many spaces are removed by trimIndent()?
AAll spaces
B4 spaces
C2 spaces
DNo spaces
💡 Hint
Check the 'Indentation Removed' column in execution_table step 2.
At which step is the string actually printed to the console?
AStep 3
BStep 2
CStep 1
DAfter all steps
💡 Hint
Look at the 'Action' column for when printing happens in execution_table.
If the first line had no indentation, what would trimIndent() remove?
A4 spaces from all lines
BAll leading spaces from all lines
CNo spaces, because common indent is zero
DOnly spaces from the first line
💡 Hint
Recall trimIndent() removes the smallest common indent shared by all lines.
Concept Snapshot
Multiline strings use triple quotes """ to include line breaks and spaces.
trimIndent() removes the smallest common leading spaces from all lines.
This keeps relative indentation but cleans up extra spaces.
Use trimIndent() to avoid unwanted indentation in multiline strings.
Print the string to see the cleaned result.
Full Transcript
This example shows how Kotlin handles multiline strings with triple quotes. The string includes lines with indentation. When trimIndent() is called, it finds the smallest number of spaces common to all lines and removes that from each line. This keeps the relative indentation intact but removes extra leading spaces. Finally, printing the string shows the cleaned text without unwanted indentation. This helps keep code readable while formatting strings nicely.