Skip to content
Table of contents
If expression in String templates

If expression in String templates

1 min read #kotlin

You can insert a value within a String using String templates. Remember to use a $ before the identifier name:

fun main() {
    val areYouOkay = true

    println(
        "${if (areYouOkay) "okay" else "nooo"}"
    )
}
kotlin

Bonus

Use triple-quoted String to store multiline text or text with special characters.

fun main() {
val age = 5
println(
  """ // triple quotes
  {
  "name" : "Kotlin",
  "age" : $age
  }
  """
)
}
kotlin