A Kotlin multiline string is a triple-quoted literal that preserves line breaks and accepts characters such as backslashes without ordinary escaping. It makes regular expressions, paths, ASCII art, and structured test data easier to read than escaped Java-style strings. section 3.5.2 section 3.5.3
Embedding expected structured text
val expectedPage = """ <html lang="en"> <p>Hello, Kotlin!</p> </html> """.trimIndent() section 3.5.3
Use multiline strings when the literal itself should show the intended content:
- Call trimIndent() to remove blank boundary lines and the common minimal indentation introduced by code formatting. section 3.5.3
- Write Windows-style paths directly, because backslashes do not need escaping. section 3.5.3
- Do not write escape sequences such as \n inside the literal; insert an expression when an escaped Unicode character or literal dollar sign must be interpreted. section 3.5.3
- Use string templates normally, including expressions that produce characters unavailable through direct escapes. section 3.5.3
val kotlinLogo =
"""
| //
|//
|/ \
""".trimIndent()
fun main() {
println(kotlinLogo)
// | //
// |//
// |/ \
}
Shows a compact ASCII-art literal and the effect of trimIndent(). section 3.5.3
val expectedPage = """
<html lang="en">
<head>
<title>A page</title>
</head>
<body>
<p>Hello, Kotlin!</p>
</body>
</html>
""".trimIndent()
val expectedObject = """
{
"name": "Sebastian",
"age": 27,
"homeTown": "Munich"
}
""".trimIndent()
Shows multiline HTML and JSON values used as readable expected output. section 3.5.3


