A string is a primitive data type that allows for arbitrary characters. Strings may come from several sources, but string literals directly in source code must follow a few particular rules.
Bare strings are unquoted, loose strings in source code. For instance, in the code:
The words "This is a string" are considered bare strings. Each word is technically a separate string, but due to MethodScript's language feature of auto-concatenation, they form a single string, with spaces separating the words.
Strings quoted using single quotes are "normal" strings. Only a small amount of escaping is required in these strings, most all characters are taken as literal UTF-8 characters.
None of the special symbols, namely the comma and exclamation point are considered operators when quoted in this manner. Some characters in the string are still handled specially, however.
If you need a literal single quote inside of a string, you must escape it with the backslash character.
The backslash is itself a special character, and cannot be used except when followed by a pre-defined special character, as defined below. To use a literal backslash, use two backslashes.
Both double quotes and single quotes may be escaped, though double quote characters aren't necessarily needed to be escaped. Both of the following lines are the same:
An arbitrary unicode character may be inserted directly via the \u escape sequence. It should be \u, followed by the four hex digit code for that symbol. Since UTF-8 is supported directly, however, you can add the symbol directly. All the following are equivalent:
Note that using the \u escape sequence is only supported if you are hardcoding the value, for dynamic inputs, you must use
char_from_unicode(), using string
concatenation won't work.
Other escape sequences are supported:
|\t||Insert a tab character|
|\n||Insert a newline character|
|\r||Insert a carriage return character|
All other escapes are invalid, and will cause a compile error.
Double Quoted Strings
Doubly quoted strings, (referred to as "smart strings") using the
symbol, follow all the escape rules of singly quoted strings, but also have
variable interpolation. No features that a smart string provides are impossible
to do otherwise, but allow for more concise and readable code. @Variables, and simple
array accesses are allowed to be embedded in the string itself, as if they had been
concatenated in. ($Variables are not supported.) As a very simple example, consider
the following code, of which both examples are equivalent:
As you can see, the second example is much more concise, and is generally easier to read. The two examples are exactly equivalent however, in fact, double quotes are internally converted to the non-double quote equivalent during compilation.
The following parse rules apply to variables embedded in the string. If an
at sign (
@) is encountered, the parser will greedily take as many
characters as possible to form a valid variable name, (any letters, lowercase
or capital, underscores, and numbers).
If the end of the variable name may be confused with the next character in the string, you may "escape" the whole sequence using braces. The second example in the above code can be written as follows, to produce the desired results:
In double quoted strings only, if you need a literal at sign, you must escape it, using a backslash:
Double strings with variables in them are considered dynamic values, and cannot be used in cases where a static value is required, for instance, array labels. If the string does not contain any variables, it works the same as a singly quoted string.