All lessons

Working with Text

7 min · Scala

Join strings, measure them, and use s-string interpolation.

Scala · Working with Text0/5 cleared

Enemy 1 of 5

Concatenation with +

This code joins two strings with + to make one greeting and prints it. Press Run to see the joined text.

Why this works

Text values (strings) can be joined end-to-end with the same `+` operator used for addition. Scala looks at the types involved and decides whether `+` means "add these numbers" or "glue these strings together." Joining strings this way is called concatenation, and it works with any number of pieces chained in a row: `"a" + "b" + "c"` gives `"abc"`.

This is straightforward for two or three pieces, but it gets visually noisy fast once you're mixing literal text with several variables, which is exactly the problem string interpolation solves.

You
Packet Wolf
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.