0
0
Kotlinprogramming~5 mins

Multiline strings with trimIndent in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a multiline string in Kotlin?
A multiline string in Kotlin is a string enclosed in triple quotes ("""), allowing the string to span multiple lines without needing escape characters.
Click to reveal answer
beginner
What does the trimIndent() function do when used with multiline strings?
trimIndent() removes the common minimal indent from all lines in a multiline string, making the string's content aligned to the left without extra spaces.
Click to reveal answer
intermediate
How does trimIndent() help with code readability?
It allows you to indent multiline strings in your code to match the surrounding code style, but still outputs the string without those indents, keeping the string clean.
Click to reveal answer
beginner
Given this Kotlin code:<br>
val text = """
    Hello
    World
""".trimIndent()
<br>What will be the output of println(text)?
The output will be:<br>
Hello
World
<br>The trimIndent() removes the 4 spaces before each line.
Click to reveal answer
intermediate
Can trimIndent() remove different amounts of spaces on each line?
No, trimIndent() removes the smallest common indent from all lines. Lines with less indent are not changed, and lines with more indent lose only the common part.
Click to reveal answer
What symbol is used to start and end a multiline string in Kotlin?
A`
B"
C"""
D'
What does trimIndent() do to a multiline string?
AAdds indentation to all lines
BConverts the string to uppercase
CRemoves all spaces from the string
DRemoves the smallest common indent from all lines
If a multiline string has lines with different indents, how does trimIndent() behave?
ARemoves all indentation from every line
BRemoves the smallest common indent from all lines
CRemoves indentation only from the first line
DLeaves the string unchanged
Why is trimIndent() useful when writing multiline strings in code?
AIt lets you indent strings in code for readability without affecting the actual string content
BIt encrypts the string content
CIt converts the string to a list
DIt adds line numbers to the string
What will this code print?<br>
val s = """
  Kotlin
    Rocks
""".trimIndent()
println(s)
AKotlin Rocks
B Kotlin Rocks
CKotlin Rocks
DskcoR niltoK
Explain how trimIndent() works with multiline strings in Kotlin and why it is helpful.
Think about how indentation in code can affect string output.
You got /3 concepts.
    Describe a situation where using a multiline string with trimIndent() would be better than a regular string.
    Consider writing a message or template inside code.
    You got /3 concepts.