Lexical
String literals
Quoted strings, escapes, ${} interpolation, raw r"..." strings and triple-quoted multiline strings.
since 0.1.0-alpha.1linuxwindowswasm
Description
A string literal is written between double quotes ("hello") or single quotes ('hello'). Both quote styles produce a string and behave identically; the only difference is which quote character must be escaped inside. An ordinary string literal must end on the line where it starts.
Inside a string:
- A backslash starts an escape sequence (listed below).
${expression}evaluates the expression and inserts its text. The value is converted by calling itstoString()method, so the expression must have one: numbers,bool,charandstringdo, and so does any class that declarestoString().
Two further forms change these rules: a raw string r"..." and a triple-quoted string """...""".
Escapes and interpolation
console.writeln("tab:\there");
console.writeln('single \'quoted\' and "double"');
console.writeln("backslash: \\");
console.writeln("\x41\x42");
console.writeln("snowman: \u{2603}");
string z = "a\0b";
console.writeln("nul length: ${z.length()}");
int n = 3;
console.writeln("n is ${n}, next is ${n + 1}");
console.writeln("cost: \${n}");
tab: here
single 'quoted' and "double"
backslash: \
AB
snowman: ☃
nul length: 3
n is 3, next is 4
cost: ${n}
Rules
Escapes (both quote styles):
\nnewline,\ttab,\rcarriage return,\0NUL. Strings hold arbitrary bytes, so an embedded NUL does not end the string:"a\0b".length()is3.\xNNis the byte with that value; it takes exactly two hexadecimal digits.\u{H}inserts the UTF-8 encoding of a Unicode scalar, with one to six hexadecimal digits. A surrogate (D800toDFFF) or a value above10FFFFproduces U+FFFD, the replacement character, rather than an error.\",\',\\and\$produce the character itself. Any other backslash followed by a charactercalso producesc; an unknown escape is not an error, so\qis justq.- A malformed
\x(not exactly two hexadecimal digits, as in\x4or\xZZ) or\u(no digits, or no closing}) is not an escape: the letter passes through and the following characters are read as ordinary text.
Interpolation:
- The hole runs from
${to the closing}. It may hold any expression, such as${n + 1}or${name.toUpper()}. - A string literal cannot appear inside a hole. Put the text in a variable first.
${}with nothing inside is a compile error.- A
$not followed by{is an ordinary character. To write${literally, escape the dollar sign:\${. - A value of an optional type such as
int?must be narrowed before it is interpolated.
Raw strings r"..." and r'...':
- The
rmust touch the opening quote. Backslashes are ordinary characters, and there is no escape processing and no${...}interpolation. - The literal ends at the next quote character of the same kind. There is no way to put the delimiting quote inside a raw string.
- A raw string is a single line.
Triple-quoted strings """...""" and '''...''':
- The literal may span several lines and may contain a single or doubled quote character of its own kind. Only three in a row end it.
- Escapes and
${...}interpolation work as in an ordinary string. - A triple-quoted literal is always a
string; it never becomes achar, even if it holds one character.
Examples
Raw strings keep backslashes and ${ as written, which suits file paths and regular expressions:
Raw strings
int n = 3;
console.writeln(r"C:\temp\new ${n}");
console.writeln(r'a\nb');
console.writeln("a\nb");
C:\temp\new ${n}
a\nb
a
b
A triple-quoted string keeps its line breaks, may contain quote characters, and still interpolates:
Multiline strings
int n = 3;
console.writeln("""line one
line two "quoted" and ""two""
n=${n}\tend""");
console.writeln('''x
y''');
line one
line two "quoted" and ""two""
n=3 end
x
y
Malformed and invalid escapes pass through instead of failing:
Lenient escapes
console.writeln("\q|\x4|\xZZ");
console.writeln("\u{zz} \u{41");
console.writeln("\u{D800}".length());
q|x4|xZZ
u{zz} u{41
3
Notes
A string's length() counts bytes, not characters. The string type and its methods are described in std.string.
See also
- Char literals — A single-quoted literal becomes a
charwhen the context expects one and it holds exactly one Unicode scalar. - string — An immutable sequence of text, stored as UTF-8.