Start Functions Events Constants Types Language Articles

String type

The string type represents a sequence of characters. Character support varies between LSO and Mono: LSO supports any byte sequence that does not contain an ASCII NUL character, even though some of them are not representable in Unicode, while Mono only supports valid Unicode sequences (also not containing NUL). There is no limit to the length of a string or string literal, other than available memory.

A string literal is written by enclosing it in double quotes. It may be empty, and does not need to end at the end of the line, but if there are newlines embedded in the literal, they will be part of it. Here are some examples:

""

"This is a string literal"

"☑ Check!"

"A line
and another line"

These four examples are string literals representing the empty string, the string This is a string literal, the string ☑ Check! and the string A line followed by a newline character and by and another line.

Inside the double quotes, there are two special characters: the double quote itself ", which obviously closes the string, and the backslash character \. The double quote closes the string literal, unless preceded by a backslash. The backslash is used as a prefix for any character that is to be interpreted literally, including the backslash itself and the double quote. The exceptions are if the backslash is followed by a lower case n or t. A backslash followed by an n is interpreted as a newline, and a backslash followed by a t is interpreted as four spaces. Examples:

"Press \"OK\" to confirm"

"\tThis is indented by 4 spaces"

"\\o/\n|\n/ \\"

"a\nb"

"a
b"

The first string literal represents the string Press "OK" to confirm; the second string literal represents a string that has four spaces followed by This is indented by 4 spaces, and the third one represents the string \o/ followed by a newline, then a |, another newline, and then the string / \, which in hovering text may look like this:

\o/
|
/ \

The last two examples represent the same string, namely the letter a followed by a newline and the letter b.

Note that indentation spaces will become part of the string if used inside a string literal, for example:

default
{
    state_entry()
    {
        llOwnerSay("String split.
                    Indented part.");
    }
}

will represent the string literal String split. followed by a newline, then twenty spaces, then Indented part.

Finally, string literals may be immediately preceded by the upper case letter L (with no spaces in between). The intention seemed to be to ignore it, but due to a bug in the compiler, it has a funny side effect: the first double quote character is taken as part of the string. For example, the string literal L"String" actually represents the string "String.

Type casting of strings

The string type is universal in the sense that it may be cast to and from every other type:

Concatenation

Two or more strings can be concatenated, meaning to make a larger string which contains the first one followed by the second and so on. The operator for concatenation is the plus sign +. Concatenation always happens at run time; the LSL compiler does not optimize the strings into a single one as other compilers do.

For example, "s" + "tick" will produce the string stick. A somewhat more useful complete example:

string-concatenation-example.lsl
default
{
    touch_start(integer n)
    {
        llSay(0, "Touched by " + llDetectedName(0));
    }
}

If e.g. Philip Linden touches it, the object will say, Touched by Philip Linden.

There is no automatic type conversion when concatenating strings. That means that if a plus sign is between a string and something of any other type (except list) in any order, a type mismatch error will be generated. For example: llOwnerSay("Your key is " + llGetOwner()); will result in a type mismatch, because llGetOwner returns a key, and keys and strings can't be concatenated. You need to add an explicit type cast, e.g. llOwnerSay("Your key is " + (string)llGetOwner());

If one of the elements is a list, the result is not a string, but a list, and a type mismatch may result at a latter point if a string was expected. See list for details.

See Also