Skip to content
Strings and Templates
step 1/5

Reading — step 1 of 5

Learn

~1 min readStrings and Null Safety

Strings are immutable, indexable, and use double quotes. Triple-quoted raw strings ("""...""") preserve newlines and don't process escapes.

val s = "hello"
println(s.length)               // 5
println(s.uppercase())          // HELLO
println(s + ", world")          // hello, world
println(s.contains("ell"))      // true

String templates embed expressions with $ (or ${expr} for non-trivial expressions):

val name = "Alice"
val age = 30
println("$name is $age years old")
println("Next year: ${age + 1}")

Most things you'd reach for String.format for can be done with templates. Kotlin's standard library has a huge String API — see kotlin.text for split, substringAfter, padEnd, etc.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…