Kotlin

String Templates

admin by @admin ADMIN
1h ago
Jun 1, 2026
Public
0 0 up · 0 down Sign in to vote
Embed expressions inside double-quoted strings with `$var` or `${expr}`. No printf/format needed for 95% of cases. Raw strings (triple-quoted) skip escape processing for multiline / regex / SQL.
Kotlin
Raw
fun main() {
    val name = "Alice"
    val age  = 30

    // Simple variable interpolation
    println("Hello, $name!")                        // Hello, Alice!

    // Expression interpolation with ${ ... }
    println("$name is ${age * 12} months old")      // Alice is 360 months old

    // Access properties/methods directly
    println("name has ${name.length} letters")

    // Raw strings — no escape processing, can span multiple lines
    val sql = """
        SELECT id, name
        FROM   users
        WHERE  name = '$name'
        ORDER  BY created_at DESC
    """.trimIndent()
    println(sql)

    // Inside raw strings, literal $ is "$" or just escape with \$
    val price = """The price is ${'$'}9.99"""
    println(price)                                  // The price is $9.99
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.