All lessons

Working with Text

7 min · Swift

Join strings, measure them, and use string interpolation.

Swift · Working with Text0/5 cleared

Enemy 1 of 5

This joins two strings with + and prints the result as one sentence. The pieces are "Hello, " and "Ada". Press Run to see them combined.

Why this works

Text in Swift is stored as a String value, written between double quotes. You can join two strings together with +, the same operator you use for adding numbers: Swift recognizes that + between two String values means "join them together" rather than "add them numerically."

Using + repeatedly works fine for joining two or three pieces, but it gets awkward once you're weaving several values (some text, a name, a number, more text) into a single sentence. That's precisely the problem the next section, string interpolation, is designed to solve more elegantly.

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