Kotlin String Templates


Both escaped strings and raw strings can contain template expressions. Template expression is a piece of code which is evaluated and its result is concatenated into string. It starts with a dollar sign $ and consists of either a variable name:

val i = 10
val s = "i = $i" // evaluates to "i = 10"

Or an arbitrary expression in curly braces:

val s = "abc"
val str = "$s.length is ${s.length}" // evaluates to "abc.length is 3"

To include a literal dollar sign in a string, escape it using a backslash:

val str = "\$foo" // evaluates to "$foo"

The exception is raw strings, which do not support escaping. In raw strings you can use the following syntax to represent a dollar sign.

val price = """