Unofficial LSL Reference

[[types:string]]


Unofficial LSL reference

User Tools

Login

You are currently not logged in! Enter your authentication credentials below to log in. You need to have cookies enabled to log in.

Login

Forgotten your password? Get a new one: Set new password

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:

  • (string)integer_value converts it to decimal. See Type casting an integer to/from other types for details.
  • (string)float_value converts it to decimal with six significant digits. See Type casting a float to string for details.
  • (string)key_value converts a key to string. The key type acts like a string (though it is intended for other uses), therefore the conversion is literal.
  • (string)vector_value converts the vector to a string with three comma-separated floats enclosed in angle brackets, but with five decimal places instead of six. See Type casting a float to string for details.
  • (string)rotation_value does the same with the four values of the rotation.
  • (string)list_value converts the list by casting every element to string and then concatenating them all together. That is, it does the same as llDumpList2String(list_var, "") (see llDumpList2String for more information). In this case, however, vectors and rotations are converted with six decimal places per float.
  • (integer)string_value tries to parse the string as an integer. See Type casting an integer to/from other types for details. If the parsing fails, 0 is the result instead.
  • (float)string_value tries to parse the string as a float. The expected format is that from the glibc function strtof. See glibc: Parsing of Floats for details. Hexadecimal floats, as specified there, are supported, but subnormal a.k.a. denormal floats in hexadecimal notation are only supported in LSO. If the parsing fails for whatever reason, 0.0 is the result instead.
  • (key)string_value converts the string to key. The resulting value may not be a valid UUID, but the key type is still able to hold any string, so the conversion is lossless.
  • (vector)string_value tries to parse the string as a vector. It expects a < followed by three comma-separated floats (see above for the format of the expected float). If the parsing fails at any point, ZERO_VECTOR is the result instead. For example, (vector)"<1, 2, 3, 4>" gives the vector <1.0, 2.0, 3.0> (the parsing succeeds after it successfully parses the third complete number), but (vector)"<1 , 2, 3>" gives ZERO_VECTOR (because there's a space before a comma). The strings inf, infinity, and nan (case insensitive) are also valid values that can appear instead of the numeric part of each component (apart from the optional sign and the leading space). If the last element's numeric part is any of the case-insensitive strings infi, infin, inifini or infinit, the parsing will fail, despite having the valid substring inf at the beginning of the numeric part.
  • (rotation)string_value is similar to vector. If the parsing fails at any point, ZERO_ROTATION is the result instead.
  • (list)string_value returns a list with one single element of type string.

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