Joining strings with +
Concatenation means joining strings end to end to make a new one. In Kotlin the + operator does it:
val message: String = "I love coding. " + "It is fun!"
println(message)
I love coding. It is fun!
The result is a new string. Neither original string is modified; Kotlin strings can't be changed after they are created. Notice that nothing is added between the two pieces. The space after coding. is part of the first literal. If it were missing, the output would read coding.It.
Why concatenation is useful
Joining two literals is pointless, because you could write the longer string directly. Concatenation matters when part of the text comes from a variable:
val favoriteAnimal: String = "parrot"
val message: String = "My favorite animal is the " + favoriteAnimal
println(message)
My favorite animal is the parrot
Change favoriteAnimal to "dog" and the same line builds a different sentence. The quoted part stays fixed, and the variable supplies the part that changes. Inside the line, the variable is written without quotes: quotes would make it the literal text favoriteAnimal instead of its value.
Chaining several pieces
You can put as many + operators in one expression as you need:
val animal: String = "parrot"
val name: String = "Andy"
println(name + ", that's a nice " + animal + " you got there!")
Andy, that's a nice parrot you got there!
Kotlin evaluates the pieces from left to right, joining two at a time. The comma, the spaces and the exclamation mark all live inside the literals, because Kotlin adds nothing on its own. When the output looks glued together, a missing space inside a literal is the usual reason.
The exercise
The starter code declares two strings:
val name: String = "John Doe"
val language: String = "Python"
The task is to print one sentence built from four pieces: "My name is ", the variable name, " and I code in " and the variable language. Build it from the front:
- Start with the first literal,
"My name is ". It ends with a space, because the name follows directly. - Add
namewith+. This is the variable, outside any quotes. - Add the second literal,
" and I code in ". It starts and ends with a space, one on each side of the wordandand before the language. - Add
language. The sentence ends with the value, so there is nothing more to append. - Wrap it all in
println, so it is printed once.
val name: String = "John Doe"
val language: String = "Python"
println("My name is " + name + " and I code in " + language)
This prints My name is John Doe and I code in Python. The sentence follows the variables: change name to "Ada" and language to "Kotlin" and the same println line prints My name is Ada and I code in Kotlin.
Building a string in steps
A message can also be assembled over several lines. With a var, the += operator appends to the existing text:
var greeting = "Hello"
greeting += ", world"
greeting += "!"
println(greeting)
Hello, world!
Each += builds a new string from the old one plus the addition and stores it back in greeting. The old string is not edited, because strings can't be edited. Declare the variable with val and the first += is rejected with 'val' cannot be reassigned. For a handful of pieces this is fine; the rule of thumb is to put everything in one expression when you can.
Gotchas
- Spaces are your job. Put them inside the literals. Count them at every joint between a literal and a variable.
- Variables go outside the quotes.
"My name is name"prints the wordname, not its value. - The left operand sets the rule. With a string on the left,
+accepts almost any value on the right and converts it to text."Age: " + 29givesAge: 29. With a number on the left, such as29 + " years", the code doesn't compile, becauseInthas no+that takes a string. - Numbers added to text.
"Total: " + 1 + 2givesTotal: 12, since the pieces are joined left to right. To add first, use parentheses:"Total: " + (1 + 2)givesTotal: 3. - String templates. Kotlin can also put a variable straight into a string with
$:"My name is $name and I code in $language"produces the same text with less punctuation. It's usually the more readable form for longer sentences, and the lesson on variables uses it. This exercise practises+.
Key takeaways
+between strings joins them into a new string.- It adds no spaces; they must be inside the quotes.
- Variables are written without quotes and supply the part that changes.
- Several
+operators chain from left to right.