Difference between revisions of "Category:LSL String"

From Second Life Wiki
Jump to navigation Jump to search
m (<lsl> examples)
Line 25: Line 25:
|
|
String examples:
String examples:
<pre>"Hello Avatar!"
<lsl>"Hello Avatar!"
"Yes"
"Yes"
"No"
"No"
Line 38: Line 38:
we all scream,
we all scream,
for ice-cream!"
for ice-cream!"
</pre>
</lsl>
|}
|}


Line 53: Line 53:
== Variable: string {{LSL Param|name}}; ==
== Variable: string {{LSL Param|name}}; ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
<pre>string name;</pre>
<lsl>string name;</lsl>
Declares a variable of type string named '''name''', with the value {{HoverText|""|empty string}}
Declares a variable of type string named '''name''', with the value {{HoverText|""|empty string}}
{|
{|
Line 63: Line 63:
== Variable: string {{LSL Param|name}} {{=}} {{LSL Param|value}}; ==
== Variable: string {{LSL Param|name}} {{=}} {{LSL Param|value}}; ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
<pre>string name = value;</pre>
<lsl>string name = value;</lsl>
Declares a variable of type string named '''name''', with the value '''value'''.
Declares a variable of type string named '''name''', with the value '''value'''.
{|
{|
Line 74: Line 74:
== [[Typecast]]: (string){{LSL Param|value_t|value}} ==
== [[Typecast]]: (string){{LSL Param|value_t|value}} ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
<pre>(string)value</pre>
<lsl>(string)value</lsl>
Converts '''value''' to a value of type string.
Converts '''value''' to a value of type string.
{|
{|
Line 88: Line 88:
<div id="box" style="padding-left: 0.5em;padding-right: 0.5em;padding-bottom: 0.5em;">
<div id="box" style="padding-left: 0.5em;padding-right: 0.5em;padding-bottom: 0.5em;">
=== Combine: {{LSL Param|value|value1}} + {{LSL Param|value|value2}} ===
=== Combine: {{LSL Param|value|value1}} + {{LSL Param|value|value2}} ===
<pre>(value1 + value2)</pre>
<lsl>(value1 + value2)</lsl>
Combines two strings into a single string without modifying the inputs. Appends '''value2''' to '''value1''' and returns the resulting string.
Combines two strings into a single string without modifying the inputs. Appends '''value2''' to '''value1''' and returns the resulting string.
{|
{|
Line 98: Line 98:


=== Comparison: {{LSL Param|value|value1}} <nowiki>==</nowiki> {{LSL Param|value|value2}} ===
=== Comparison: {{LSL Param|value|value1}} <nowiki>==</nowiki> {{LSL Param|value|value2}} ===
<pre>(value1 == value2)</pre>
<lsl>(value1 == value2)</lsl>
Compares two strings, returns {{HoverText|one|1}} if same length and same characters, else returns zero.
Compares two strings, returns {{HoverText|one|1}} if same length and same characters, else returns zero.
This operator works exactly like <code>!strcmp('''value1''', '''value2''')</code> in C, thus technically differs from the counterintuitive behavior of the == operator in C and in Java.
This operator works exactly like <code>!strcmp('''value1''', '''value2''')</code> in C, thus technically differs from the counterintuitive behavior of the == operator in C and in Java.
Line 108: Line 108:
<div id="box" style="padding-left: 0.5em;padding-right: 0.5em;padding-bottom: 0.5em;">
<div id="box" style="padding-left: 0.5em;padding-right: 0.5em;padding-bottom: 0.5em;">
=== Comparison: {{LSL Param|value|value1}} != {{LSL Param|value|value2}} ===
=== Comparison: {{LSL Param|value|value1}} != {{LSL Param|value|value2}} ===
<pre>(value1 != value2)</pre>
<lsl>(value1 != value2)</lsl>
Compares two strings, returns {{HoverText|zero|0}} if same length and same characters, otherwise non-zero.
Compares two strings, returns {{HoverText|zero|0}} if same length and same characters, otherwise non-zero.
This operator works exactly like <code>strcmp('''value1''', '''value2''')</code> in C, thus technically differs from the counterintuitive behavior of the != operator in C and in Java.
This operator works exactly like <code>strcmp('''value1''', '''value2''')</code> in C, thus technically differs from the counterintuitive behavior of the != operator in C and in Java.
Line 122: Line 122:
== Examples ==
== Examples ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
<pre>
<lsl>integer int = 48934;
integer int = 48934;
string str = (string)int;
string str = (string)int;
string str_2;
string str_2;
str_2 = str;
str_2 = str;</lsl>
</pre>
</div></div>
</div></div>
<div id="box">
<div id="box">

Revision as of 20:06, 31 December 2007

A string is text data. The length of a string is only limited by available script memory. 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.

Strings can be concatenated using the + operator.

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

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>

Note: Escape codes (listed above) are translated when the script is compiled, and not while it's running. The result: 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 in any case. You'll have to do that yourself, if you really really need it for some reason.

Note: People who come to LSL from C and Java and such languages may find these LSL string escape rules confusing while new. LSL "\n" means llUnescapeURL("%0A"), same as C and Java, but "\t" means llUnescapeURL("%20%20%20%20") rather than llUnescapeURL("%09"), and "\r" means "r" rather than llUnescapeURL("%0D"), etc.


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

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