Kotlin - Data Type String templates

Introduction

Kotlin can do string concatenation to mix expressions with string literals:

Demo

fun main(args: Array<String>) {
        val name = "hi" 
        val concat = "hello " + name 
        //  ww  w. ja  v a 2 s .  c om
        prinln(concat)

}

String templates can handle embedded values, variables, or even expressions.

It is also called the string interpolation.

A value or variable can be embedded simply by prefixing with a dollar ($) symbol:

Demo

fun main(args: Array<String>) {
        val name = "Jack" 
        val str = "hello $name" 
        println(str)/*w  ww .  j  a v  a2s  .  co m*/

}

Arbitrary expressions can be embedded by prefixing with a dollar ($) and wrapping in braces {}:

Demo

fun main(args: Array<String>) {

        val name = "java2s.com" 
        val str = "hello $name. Your name has ${name.length} characters" 
        /*from  w  ww .java 2s . c  om*/
        println(str)
}