Kotlin has two types of string literals:
Escaped string handles special characters by escaping them. Escaping is done with a backslash. The following escape sequences are supported: \\t
, \\b
, \\n
, \\r
, \\'
, \\"
, \\\\
and \\$
. To encode any other character, use the Unicode escape sequence syntax: \\uFF00
.
val s = "Hello, world!\\n"
Raw string delimited by a triple quote """
, contains no escaping and can contain newlines and any other characters
val text = """
for (c in "foo")
print(c)
"""
Leading whitespace can be removed with trimMargin() function.
val text = """
|Tell me and I forget.
|Teach me and I remember.
|Involve me and I learn.
|(Benjamin Franklin)
""".trimMargin()
Default margin prefix is pipe character |
, this can be set as a parameter to trimMargin; e.g. trimMargin(">")
.