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
$peakdrops a value straight into the literal.${(meters * 3.281).toInt()}runs any expression inline.- Triple quotes keep line breaks;
trimMargincuts everything up to each pipe.
Keywords and builtins used here
funmainval
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.
Step 3 of 3 in Values, step 3 of 27 in Language basics.