String Templates in Kotlin

From the Kotlin cheat sheet · Getting Started · verified Jul 2026

String Templates

String interpolation and manipulation

kotlin
// String templates
val name = "Alice"
val age = 30
val message = "Hello, $name! You are $age years old."
val expression = "Next year you'll be ${age + 1}"

// Multiline strings
val multiline = """
    |First line
    |Second line
    |Third line
    """.trimMargin()

val rawString = """
    No escaping needed: 
 	 \
    Preserves formatting
"""

// String operations
val str = "Kotlin"
println(str.length)  // 6
println(str.uppercase())  // KOTLIN
println(str[0])  // K
println(str.substring(0, 3))  // Kot
💡 Use $ for simple variables, ${} for expressions
⚡ Triple quotes for multiline strings without escaping
📌 trimMargin() removes leading whitespace with | delimiter
🟢 buildString is efficient for complex string construction

More Kotlin tasks

Back to the full Kotlin cheat sheet