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 +

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...

Ln 1, Col 1Spaces: 4
Sign up to fightTarget output: Hello, Ada