Literals/String: Difference between revisions

no edit summary
(plainTeX)
No edit summary
Line 85:
Character literals:
 
<lang d>char c = 'a';</dlang>
 
Regular strings support C-style escape sequences.
 
<lang d>auto str = "hello"; // UTF-8
auto str2 = "hello"c; // UTF-8
auto str3 = "hello"w; // UTF-16
auto str4 = "hello"d; // UTF-32</dlang>
 
Literal string (escape sequences are not interpreted):
 
<lang d>auto str = `"Hello," he said.`;
auto str2 = r"\n is slash-n";</dlang>
 
Specified delimiter string:
 
<lang d>// Any character is allowed after the first quote;
// the string ends with that same character followed
// by a quote.
auto str = q"$"Hello?" he enquired.$";</dlang>
 
<lang d>// If you include a newline, you get a heredoc string:
auto otherStr = q"EOS
This is part of the string.
So is this.
EOS";</dlang>
 
Token string:
 
<lang d>// The contents of a token string must be valid code fragments.
auto str = q{int i = 5;};
// The contents here isn't a legal token in D, so it's an error:
auto illegal = q{@?};</dlang>
 
Hex string:
 
<lang d>// assigns value 'hello' to str
auto str = x"68 65 6c 6c 6f";</dlang>
 
=={{header|E}}==
Line 135:
Strings and characters use syntax similar to Java; double and single quotes, respectively, and common backslash escapes.
 
Quasiliterals are a user-extensible mechanism for writing any type of object "literally" in source, with "holes" for interpolation or pattern-matching. The fourth example above represents a Term object (terms are a tree language like [[XML]] or [[JSON]]), with the items from the variable <var><codett>adjectives</codett></var> spliced in.
 
Quasiliterals can be used for strings as well. The third example above is the built-in simple interpolator, which also supports pattern matching. There is also a regular-expression quasi-pattern:
Line 242:
'Hello, ',NAME,' you may have already won $1,000,000'
 
For multiline literals, you may define an explicit noun, which is terminated by a lone <codett>)</codett> in the very first column:
 
template =: noun define