Start learning free

LESSON 56 OF 414 · STRINGS I

String Concatenation in Scala: Appending with +=

Lesson Read the full explanation

Remember that string concatenation is used to dynamically generate messages that can be different depending on context.

We most often use it with variables, let's check an example:

var adjective = "awesome"
var message = "AlgoCademy is "

// String concatenation & assignment:
message = message + adjective

println(message) // Output: AlgoCademy is awesome

We first concatenate message and adjective and then we assign the result to message.


Concatenating with +=

Since this is such a common pattern, there is the += operator which does both the concatenation and assignment in one step.

var adjective = "awesome"
var message = "AlgoCademy is "

// With += operator:
message += adjective

println(message) // Output: AlgoCademy is awesome

Concatenating multiple strings:

We can append as many strings as we want using the += operator:

var name = "Andy"
var pet = "dog"
var message = "Hey, "

message += name
message += "! Nice " 
message += pet
message += "!" 

println(message) // Output: Hey, Andy! Nice dog!


Assignment
Follow the Coding Tutorial and let's practice with string concatenation!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    SCALA · STRINGS I

    String Concatenation Ii in Scala, explained

    Building messages from variables

    Concatenation joins strings with +. It is most useful with variables, because the same code can then produce different messages depending on what the variables hold:

    var adjective = "awesome"
    var message = "AlgoCademy is "
    
    message = message + adjective
    
    println(message) // AlgoCademy is awesome
    

    The right side, message + adjective, is evaluated first and produces a new string. The assignment then stores it in message, replacing the old value.

    The += operator

    Updating a variable with its own value plus something else is so common that Scala has a shorthand. message += adjective means exactly message = message + adjective:

    var adjective = "awesome"
    var message = "AlgoCademy is "
    
    message += adjective
    
    println(message) // AlgoCademy is awesome
    

    You can use it as many times as you like to build a message in pieces:

    var name = "Andy"
    var pet = "dog"
    var message = "Hey, "
    
    message += name
    message += "! Nice "
    message += pet
    message += "!"
    
    println(message) // Hey, Andy! Nice dog!
    

    Each += adds to the end of whatever message holds at that moment.

    Why var matters here

    += reassigns the variable on its left, so that variable must be declared with var. A val can't be reassigned, and using += on one is a compile error: the compiler reports that += is not a member of String.

    Strings are immutable in Scala. A += doesn't change the existing string; it creates a new, longer one and makes the var point to it. You can see this with a second variable:

    val s = "ab"
    val t = s + "c"
    println(s) // ab
    println(t) // abc
    

    The right side of += doesn't have to be a string. Appending a number turns it into text:

    var n = "n"
    n += 5
    println(n) // n5
    

    Watch the order of evaluation when you mix numbers and text. + works left to right, so 1 + 2 + "a" is "3a", while "a" + 1 + 2 is "a12".

    When to prefer something else

    Long chains of += are easy to read in a lesson but clumsy in real code. For a message with a few variables in it, Scala's string interpolation, written s"Hey, $name! Nice $pet!", builds the same text in one expression. Repeated += in a loop also creates a new string each time, so for many pieces a StringBuilder is the usual tool. For the small messages in this lesson, += is exactly right.

    The exercise

    The starter code gives you two variables, a var message that holds "Coding is " and a val adjective that holds "worthwhile". The three steps are:

    1. Append adjective to message with +=. Only message is a var, and message is the one being changed, so this is allowed. The val is only read.
    2. Append ", isn't it?" with +=. The apostrophe needs no escape inside double quotes, and the leading comma and space are part of the string.
    3. Print message with println.

    The complete solution:

    var message = "Coding is "
    val adjective = "worthwhile"
    
    message += adjective
    message += ", isn't it?"
    println(message)
    

    It prints:

    Coding is worthwhile, isn't it?
    

    Common mistakes

    • Forgetting the spaces. Concatenation adds nothing between the pieces. The space after "Coding is " is what separates is from worthwhile.
    • Printing before appending. println shows the value at that moment, so a print before the += lines would show Coding is only.
    • Using += on a val. The compiler rejects it. Declare the variable that grows with var.
    • Putting the name in quotes. message += "adjective" appends the word "adjective", not the contents of the variable.

    Key takeaways

    • message += x is shorthand for message = message + x.
    • The variable on the left must be a var.
    • Strings are immutable: each += creates a new string.
    • Pieces are joined exactly as they are, so include any spaces and punctuation you need.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free