Category:LSL String

From Second Life Wiki
Jump to navigation Jump to search

A string is text data.

String values are enclosed in double quotes when defined in LSL text.

Any character may be used in a string though some will need to be escaped (see Escape Codes section on this page for more info.)

The length of a string is only limited by available script memory.

Strings can be concatenated (joined together) using the + operator.

String examples: <lsl>"Hello Avatar!" "Yes" "No" "It's 10 o'clock." "I am 21 years old!" "Help " + "me" EOF //The following two strings have the same value. "I scream,\nyou scream,\nwe all scream,\nfor ice-cream!" "I scream, you scream, we all scream, for ice-cream!" </lsl>


Variable: string name;

<lsl>string name;</lsl> Declares a variable of type string named name, with the value ""

• variable name variable name

Variable: string name = value;

<lsl>string name = value;</lsl> Declares a variable of type string named name, with the value value.

• variable name variable name
• expression value string expression or constant

Typecast: (string)value

<lsl>(string)value</lsl> Converts value to a value of type string.

• expression value expression or constant

Operators

See Operators for more information.

Combine: value1 + value2

<lsl>(value1 + value2)</lsl> Combines two strings into a single string without modifying the inputs. Appends value2 to value1 and returns the resulting string.

• expression value1 string expression or constant
• expression value2 string expression or constant

Comparison: value1 == value2

<lsl>(value1 == value2)</lsl> Compares two strings, returns one if same length and same characters, else returns zero. This operator works exactly like !strcmp(value1, value2) in C, thus technically differs from the counterintuitive behavior of the == operator in C and in Java.

• expression value1 string expression or constant
• expression value2 string expression or constant

Comparison: value1 != value2

<lsl>(value1 != value2)</lsl> Compares two strings, returns zero if same length and same characters, otherwise non-zero. This operator works exactly like strcmp(value1, value2) in C, thus technically differs from the counterintuitive behavior of the != operator in C and in Java.

• expression value1 string expression or constant
• expression value2 string expression or constant

Escape Codes

Substring Replaced with
\t four spaces
\n new line
\" double quote
\\ backslash

Escape codes are translated when the script is compiled; not while it's running. The result is that only strings that are inside your script when it is compiled will get, say, \n turned into a "new line" character. Text you read in from a notecard, chat, http, etc, will not be checked for escape codes -- that same \n typed in a notecard doesn't automatically turn into a "new line" character. You'll have to do that yourself, if you really really need it for some reason.

Those who are coming LSL from such languages as C and Java may find these LSL string escape rules confusing at first. In LSL, "\n" means llUnescapeURL("%0A"), as it does in C and Java, but "\t" means llUnescapeURL("%20%20%20%20") rather than llUnescapeURL("%09"), and "\r" means "r" rather than llUnescapeURL("%0D"), etc.

String Length Limitations

As noted at the start of this page, the length of a single string is only limited by available script memory. However, there are some operational limitations.

llSay: When using llSay, any text string being said will be truncated to 1023 bytes if it is greater than that. This happens "silently." (i.e. you get no error notice, nor any indication that it has been done.)

Notecard lines: When reading in lines from notecards, lines longer than 255 characters will be truncated silently as well.

llMessageLinked: It appears that the length of the string element being passed in a link message is not affected by either of the above two limitations, and that it is indeed limited only to script memory.


Characters with Accents

It used to be that your script would not compile if you included in it a string containing a character with an accent, such as "écoutez", or punctuation not used in English, such as "¿Entiende?".

Sometime in late spring / early summer 2008, scripts began compiling with accents in text strings.

That is to say:

string hello = "Avatar écoutez";

now works (noticed as of July 2008 by the present writer.)


Examples

<lsl>integer int = 48934; string str = (string)int; string str_2; str_2 = str;</lsl>

Useful Functions

String functions in the CombinedLibrary

•  str_replace replace all instances of a string with another string in a target string.
•  TrimRight Trim characters from the right end of a string
•  TrimLeft Trim characters from the left end of a string
•  TrimBoth Trim characters from the both ends of a string

Examples

•  SplitLine Insert 'new line' escape codes at certain positions of a string