Got a question that the wiki doesn't answer? Ask on the forum (preferred), or join us on IRC.



From Wiki
Jump to: navigation, search

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

Note: 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:

1   msg(This is a string);

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.

1   msg('This is a string, with symbols in it!');

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.

1   msg('Internal quotes: \'quote\'');

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.

1   msg('Literal backslash: \\');

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:

1   msg('A \"string\"');
2   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:

1   msg('\u2665');
2   msg('♥');
3   @value = 2665;
4   msg(char_from_unicode(@value)); //For dynamic inputs

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:

Escape Sequence Description
\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:

1   @hello = 'Hello';
2   @world = 'World';
3   msg(@hello . ' ' . @world . '!');
4   msg("@hello @world!");

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).

1   msg("A @variable Here"); // == 'A ' . @variable . ' Here'
2   msg("A@variableHere"); // == 'A' . @variableHere

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:

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

In double quoted strings only, if you need a literal at sign, you must escape it, using a backslash:

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

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.

Navigation menu