You are viewing an older version of the site. Click here to view
the latest version of this page. (This may be a dead link, if so, try the root page of the docs
here.)
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 ===
{{TakeNote|text=Bare strings and auto-concatenation should be avoided, as those features will be removed in the future}}
Bare strings are unquoted, loose strings in source code. For instance, in the code:
Copy 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.
=== Quoted Strings ===
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.
Copy Code
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.
Copy Code
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.
Copy Code
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:
Copy Code
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 (
Copy Code
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:
Copy Code
In double quoted strings only, if you need a literal at sign, you must escape
it, using a backslash:
Copy Code
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.
{{LearningTrail}}
msg(This is a string);

1 {{function|msg}}(This is a {{object|string}});
msg('This is a string, with symbols in it!');

1 {{function|msg}}('This is a string, with symbols in it!');
msg('Internal quotes: \'quote\'');

1 {{function|msg}}('Internal quotes: \'quote\'');
msg('Literal backslash: \\');

1 {{function|msg}}('Literal backslash: \\');
msg('A \"string\"'); msg('A "string"');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:
msg('\u2665'); msg('♥'); @value = 2665; msg(char_from_unicode(@value)); //For dynamic inputsNote that using the \u escape sequence is only supported if you are hardcoding the value, for dynamic inputs, you must use {{function|char_from_unicode}}, using string concatenation won't work. Other escape sequences are supported: {| |- ! Escape Sequence ! Description |- | \' | Inserts a literal single quote character |- | \" | Inserts a literal double quote character |- | \\ | Inserts a literal backslash character |- | \t | Insert a tab character |- | \n | Insert a newline character |- | \r | Insert a carriage return character |- | \@ | (Only in double quoted strings) A literal @ symbol, for when this could be confused with a variable |- | \0 | Inserts the null character |- | \uxxxx | Inserts the specified UTF-8 character |- | \Uxxxxxxxx | Inserts the specified UTF-16 character |- | \f | Inserts a form feed character |- | \v | Inserts a vertical tab character |- | \a | Inserts an alarm (bell) character |- | \b | Inserts a backspace 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:
@hello = 'Hello';
@world = 'World';
msg(@hello . ' ' . @world . '!');
msg("@hello @world!");

1 @hello = 'Hello';
2 @world = 'World';
3 {{function|msg}}(@hello . ' ' . @world . '!');
4 {{function|msg}}("@hello @world!");
@
) 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).
msg("A @variable Here"); // == 'A ' . @variable . ' Here'
msg("A@variableHere"); // == 'A' . @variableHere

1 {{function|msg}}("A @variable Here"); // == 'A ' . @variable . ' Here'
2
3 {{function|msg}}("A@variableHere"); // == 'A' . @variableHere
msg("A @{variable} Here"); // == 'A ' . @variable . ' Here'
msg("A@{variable}Here"); // == 'A' . @variable . 'Here'

1 {{function|msg}}("A @{variable} Here"); // == 'A ' . @variable . ' Here'
2
3 {{function|msg}}("A@{variable}Here"); // == 'A' . @variable . 'Here'
msg("Not a \@variable"); // == 'Not a @variable'
msg("Still not a \@{variable['one']}"); // == 'Still not a @{variable['one']}'
msg("email\@email.com"); // == '[email protected]'

1 {{function|msg}}("Not a \\@variable"); // == 'Not a @variable'
2
3 {{function|msg}}("Still not a \\@{variable['one']}"); // == 'Still not a @{variable['one']}'
4
Find a bug in this page? Edit this page yourself, then submit a pull request.