Strings

The String class represents an immutable string.

For mutable strings you can use StringBuilder or StringJoiner classes from Java.

A string literal is represented by sequence of characters in double quotes like "Hello". Escaping is allowed.

A raw string is delimited by a triple quote ("""), contains no escaping and can contain newlines and any other characters. By default, | is used as margin prefix, but you can choose another character.

You can access to the character via the indexing operation like str[i].

You can iterate over string characters with a for loop.

val str="Hello World!\n"
println(str[5])

for (ch in str) {
    println(c)
}

val text1 = """
    for (c in "foo")
        print(c)
"""

val text2 = """
    |Tell me and I forget.
    |Teach me and I remember.
    |Involve me and I learn.
    |(Benjamin Franklin)
    """.trimMargin()

escape sequences

esc. seq. description
\" Inserts a double quote.
\' Inserts a single quote.
\\ Inserts a backslash.
\n Inserts a new line.
\r Inserts a carriage return.
\v Inserts a vertical tab.
\t Inserts a tab.
\f Form feed.
\uXXXX Inserts a unicode character with code XXXX (4 hex digits).

comparison

The == operator checks whether two strings are structurally equal. This is same as equals() method in Java.

The === operator checks whether two variables are pointing to the same object. This is same as == operator in Java.

templates

You can add templates into string literals. Just specify name or expression in curly braces after $ sign.

val i = 10
println("i = $i") // prints "i = 10"

val s = "abc"
println("$s.length is ${s.length}") // prints "abc.length is 3"

String

Class String have all fuctionality of Java String class.

Unlike Java, methods were added as extensions.