Algus

Strings

String Interpolation

val name = "Link"

// string concatenation
println("Hello " + name)

// string interpolation
println("Hello $name")

// string templates
println("$name.length is ${name.length}") // "Link.lenght is 4"

Raw Strings

Raw strings can contain newlines and arbitrary text. It is delimited by a triple quote ("""):

println("""
	$name is the hero
	of $place
"""
)

// To remove leading whitespace from raw strings use the `trimMargin()`
println("""
	|$name is the hero
	|of $place
""".trimMargin() 
// ^ By default, a pipe symbol `|` is used as margin prefix
)

Regular Expressions

A commonly used tool for parsing and working with strings are regular expressions.

// readLine reads a line of input from the stdin
val playerLevelInput = readLine()!! // !! is the "not-null assertion operator"

val regex = playerLevelInput.matches("""\d+""".toRegex())

playerLevel = if (regex) {
	playerLevelInput.toInt()
	} else {
		1
	}

💡 Behind the scenes, Kotlin’s Regex type uses Java’s Pattern class when targeting the JVM.