liveBook · Manning

Multiline string

As explained in Kotlin in Action, 2E

A triple-quoted string is a Kotlin string literal delimited by three quotation marks that preserves line breaks and does not interpret ordinary escape sequences.

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

How Kotlin in Action, 2E covers it

How deep it goes
Explains delimiters, indentation cleanup, line endings, escape limitations, templates, and test usage without descending into compiler internals.
Examples
Uses path-matching patterns, ASCII art, Windows paths, HTML, JSON, and expected multiline test output.
What you can do after
Build readable Kotlin literals for multiline text, regular expressions, paths, and expected structured output.
How it connects
Connects multiline strings with regular expressions, string templates, escape sequences, string literals, and automated tests.

Also explained in

Compare how 3 books explain multiline string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.