Strings
Kotlin strings are immutable. Interpolation uses \$name or \${expression}. Triple-quoted strings preserve everything literally — perfect for multi-line content.
String operations + multi-line + raw
EXAMPLE
val name = "Ada"
val age = 36
// Interpolation
val greeting = "Hello, $name! You are $age."
val message = "You scored ${score * 100 / max}%."
// Multi-line / raw — no escape interpretation
val body = """
Dear $name,
Welcome to Kotlin.
--
\n is literal here, not a newline.
""".trimIndent()
// Building
val csv = listOf("a", "b", "c").joinToString(separator = ",")
val many = buildString {
appendLine("# Report")
for ((k, v) in items) append("$k: $v\n")
}
// Common methods
"Hello, World".uppercase()
" spaced ".trim()
"camelCase".replace(Regex("[A-Z]")) { "_" + it.value.lowercase() } // 'camel_case'
"comma,separated,values".split(",")
"42".toInt() // throws if invalid
"42x".toIntOrNull() // null if invalid
// Padding + indexing
"$age".padStart(4, '0') // '0036'
name[0] // 'A'
name.first(); name.last()
Why it matters
toIntOrNull(), toDoubleOrNull(), etc., are the idiomatic safe parsers. Pair them with ?: for defaults: val n = s.toIntOrNull() ?: 0.
Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.
Example
Example
val name = "Ada"
println("Hello, $name!")
println("""
multi
line
""".trimIndent())
Try it Yourself »
Discussion
Loading…