Anvil | Smiths Smiths | Register Register | Login Login |
Search:
Show links Show tools Show tree | Previous document Next document | njet.org > Anvil > Documentation > Language reference > Lexical analysis > Literals > String literals

String literals

String literals come in several flavors. Strings start and end with a quotation character, which is either a single quote ('), a double quote ("), or a grave accent (`). No line feeds or quote marks are allowed between these characters unless preceded by a backslash, as shown below.

Long strings starts and ends with triple double quotes ("""). Long strings may span several lines and other quotation marks need not to be escaped.

Backslash strings starts with backslash (\) followed immediately by symbol. Backslash promotes symbol to string.

There may be embedded expressions inside strings.

EscapeMeaning
\\Backlash
\'Single quote
\"Double quote
\`Grave accent
\bBell
\fFormfeed
\nLinefeed
\rCarriage return
\tHorizontal tab
\xNN 8-bit unicode character in hexadecimal notation NN
\xNNN 8-bit unicode character in octal notation NNN
\uNNNN 16-bit unicode character in hexadecimal notation NNNN
\character character as is

Identifier string literal

It is also possible to quote an identifier (or a keyword) in a string, in the same manner as keywords can be quoted in identifiers. This is done by adding a backslash (\) in front of the identifier.

Implicit string concatentation

If many strings (in any format as described above) are written in succession, they will be concatenated into one string. A limitation is that there cannot be anything between strings except comments and white spaces.

Examples:
  str = "hello\nworld\n";
  str = 'hello\n' `world\n`; // same as above
  str = 'hello\n' `world\n`; // same as above
  str = \Hello \World;       // "HelloWorld"
  greeting = """Hello!
Time is now ${anvil.time.getTime()}.
Wuzzup?""";
  name = \anvil;
See also  | Literals  | Constants  | Import  | Grammar
Contributes notes:
Add a note
What's new | Anvil