If expression in String templates

less than 1 minute read

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"}"
    )
}

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
  }
  """
)
}

Tags:

Categories:

Updated: