All lessons

Working with Text

7 min · Dart

Join strings, measure them, and use string interpolation.

Dart · Working with Text0/5 cleared

Enemy 1 of 5

Concatenation with +

This joins two pieces of text with + and prints the result. Spaces are not added for you, so greeting includes one at the end. Press Run to see it.

Why this works

Dart lets you join two strings together with the + operator, the same symbol you use for adding numbers, but Dart determines from the types involved that this means "stick these two pieces of text together" rather than "add these numbers." Like most languages' concatenation operators, + doesn't add a space automatically, so if you want a gap between the joined pieces you need to include it yourself (notice greeting already ends with a trailing space in the example).

Dart accepts both single quotes and double quotes for strings, and unlike Ruby or PHP, there's no behavioral difference between them (no separate "interpolating" vs. "literal" quote style). Pick whichever is more convenient for the string you're writing, for example switching to double quotes if the text itself contains an apostrophe.

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