Strings
A String
, representing a piece of text, is a commonplace type in most programming languages, and Kotlin is no exception. However, compared to languages like Java, Kotlin offers greater expressiveness when declaring string literals.
String Quoting Options
It is fairly likely that you are used to programming languages where strings are denoted by double-quote ("
) characters, such as in our “hello, world” example:
println("hello, world!")
In Java, that is your only option. Other languages, like Ruby, offer lots of possible ways to declare strings, such as single-quoted values ('Hello, world!'
). Kotlin falls in between, offering two string quoting options: double-quotes and triple-quotes ("""
).
A double-quoted string works like its Java counterpart:
- It cannot contain newlines
- Special characters, such as newlines or tabs, need to be indicated via “escape sequences”
There are eight escape sequences supported in Kotlin:
-
\t
for tab -
\b
for backspace -
\n
for newline -
\r
for “carriage return” (if you are under 50 years old, ask your grandparents what a “carriage” was with respect to a “typewriter”) -
\'
for a single quote -
\"
for a double quote -
\$
for a dollar sign -
\\
for a backslash
Anything else can be encoded using Unicode escape sequences (e.g., \u221E
for the infinity symbol: ∞)
A triple-quoted “raw” string, on the other hand, does not support escape sequences but does support embedded newlines, tabs, and anything else. So, this Kotlin:
println("""Hello,
world!""")
produces this output:
Hello,
world!
This allows you to directly express strings that otherwise would be a mess of text and escape sequences, resulting in more readable code.
However, indents then become a problem. If your IDE wants to indent code to keep things aligned, it might add spaces or tabs inside of your string that you do not want. To help with this, you can use a trimMargin()
function on String
to eliminate unwanted indentation.
For example, let’s look at:
fun main() {
println("""Hello,
world with extra whitespace!""")
println("""Hello,
>world using >!""".trimMargin(">"))
println("""Hello,
|world using |!""".trimMargin())
}
The first println()
call results in extra whitespace being embedded in the result, as the second line is indented:
Hello,
world with extra whitespace!
The other two println()
calls use trimMargin()
. The latter of those uses the default pipe (|
) as the margin indicator, while the other uses a custom character (>
), passing that character as a parameter to trimMargin()
. Both remove our indent:
Hello,
world using >!
Hello,
world using |!
String Expressions
String concatenation works using the +
operator as you would expect:
println("Hello, " + "world!")
However, you will find a lot less string concatenation used in Kotlin than you may be used to in other languages like Java. That is because Kotlin, like Ruby, supports “string interpolation”, where expressions embedded in string literals can be evaluated directly. We will see that in action later in the book.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.