typestar

String templates in Kotlin

Interpolation with $, expressions with braces, and multiline raw strings.

fun main() {
    val peak = "Mount Tolmie"
    val meters = 1408
    // $name splices a value; ${...} splices any expression.
    println("$peak stands $meters m tall")
    println("In feet: ${(meters * 3.281).toInt()}")

    // Triple quotes keep line breaks; trimMargin strips the pipes.
    val sign = """
        |Trailhead: $peak
        |Water at km 3 and km 9
    """.trimMargin()
    println(sign)
}

How it works

  1. $peak drops a value straight into the literal.
  2. ${(meters * 3.281).toInt()} runs any expression inline.
  3. Triple quotes keep line breaks; trimMargin cuts everything up to each pipe.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
355
Tokens
66
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 57 seconds.

Type this snippet

Step 3 of 3 in Values, step 3 of 27 in Language basics.

← Previous Next →