Difference between revisions of "LlStringTrim"
Jump to navigation
Jump to search
Tapple Gao (talk | contribs) (Noted that llList2Json also trims strings) |
Frionil Fang (talk | contribs) (a much faster (~2x) and memory efficient (didn't measure exactly, but several kB on a 1 kB string) approach for removing doubled spaces; regrouped obsolete examples as "maybe don't use these") |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 31: | Line 31: | ||
{{!}}} | {{!}}} | ||
|examples= | |examples= | ||
Whenever you are accepting unstructured input from a user -- whether via chat or via a notecard -- it is a good idea to always full trim it: | Whenever you are accepting unstructured input from a user -- whether via chat or via a notecard -- it is a good idea to always full trim it: | ||
<syntaxhighlight lang="lsl2">llStringTrim("User input", STRING_TRIM);</syntaxhighlight> | |||
< | |||
This example returns the number of leading and trailing 'white space' characters on a string that were removed (not particularly useful but shows how to use the function). | This example returns the number of leading and trailing 'white space' characters on a string that were removed (not particularly useful but shows how to use the function). | ||
< | <syntaxhighlight lang="lsl2"> | ||
default | default | ||
{ | { | ||
Line 68: | Line 65: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
|helpers | |helpers | ||
|also_functions | |also_functions | ||
Line 78: | Line 75: | ||
Date of release [[ Release_Notes/Second_Life_Release/1.15#Release_Notes_for_Second_Life_1.15.0.282.29_April_25.2C_2007 | 25-04-2007 ]] | Date of release [[ Release_Notes/Second_Life_Release/1.15#Release_Notes_for_Second_Life_1.15.0.282.29_April_25.2C_2007 | 25-04-2007 ]] | ||
|notes= | |notes= | ||
Aside from white space at the beginning and / or end, the actual string will be unaffected. This means too, though, that extraneous spaces within the string -- for instance, a mistaken double-space type -- will not be corrected. | *The exact set of characters stripped are 0x09 (tab), line feed (0x0a, "\n"), vertical tab (0x0b), form feed (0x0c), carriage return (0x0d) and space (0x20, " "). | ||
** Only line feed and space function as true whitespace within SL, the others will produce some manner of a printable character. | |||
The following will remove all | ** The "\t" escape code doesn't actually represent the tab character, but several spaces instead. | ||
< | ** Other whitespace characters defined in Unicode, such as non-breaking space (0xa0) or specific-width spaces in the 0x2000 range are not stripped. | ||
* Aside from white space at the beginning and / or end, the actual string will be unaffected. This means too, though, that extraneous spaces within the string -- for instance, a mistaken double-space type -- will not be corrected. | |||
* The following will remove all doubled (or further multiplied), leading and trailing spaces with iterated [[llReplaceSubString]]: | |||
< | <syntaxhighlight lang="lsl2"> | ||
while(llStringLength(s) != llStringLength(s = llReplaceSubString(s, " ", " ", 0))); | |||
s = llStringTrim(s, STRING_TRIM); | |||
</syntaxhighlight> | |||
* llReplaceSubString is also the easiest way to remove all spaces from a string: | |||
<syntaxhighlight lang="lsl2">llReplaceSubString("some words to remove the spaces from", " ", "", 0);</syntaxhighlight> | |||
* [[llList2Json]] also trims strings contained in the list. Thus, an easy way to trim an entire list of strings is: | |||
<syntaxhighlight lang="lsl2"> | |||
list l = [" a ", " b", "c ", " d "]; | |||
list trimmedList = llJson2List(llList2Json(JSON_ARRAY, l)); | |||
</syntaxhighlight> | |||
* Some old, less efficient methods, no longer recommended for use: | |||
:* Intra-string space trimming: | |||
::<syntaxhighlight lang="lsl2">llDumpList2String(llParseString2List(src, [" "], []), " "); //works but can use a large quantity of memory</syntaxhighlight> | |||
:* An method to strip spaces from the sentence: | |||
::<syntaxhighlight lang="lsl2"> | |||
//Added By To-mos Codewarrior | //Added By To-mos Codewarrior | ||
string str1 = "some words to remove the spaces from"; | string str1 = "some words to remove the spaces from"; | ||
Line 91: | Line 103: | ||
data=llDeleteSubString(str1,index,index); | data=llDeleteSubString(str1,index,index); | ||
} | } | ||
</ | </syntaxhighlight> | ||
:* Variant of above: | |||
< | ::<syntaxhighlight lang="lsl2">(string)llParseString2List(src, [" "], []); //works but can use a large quantity of memory</syntaxhighlight> | ||
|deprecated | |deprecated | ||
|cat1=String | |cat1=String |
Latest revision as of 11:19, 13 January 2025
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
Summary
Function: string llStringTrim( string src, integer type );0.0 | Forced Delay |
10.0 | Energy |
Returns a string that is src with leading and/or trailing white space (spaces, tabs, and line feeds) trimmed from it.
• string | src | |||
• integer | type | – | STRING_TRIM* flag(s) |
Constant | Description | |
---|---|---|
STRING_TRIM_HEAD | 0x1 | Trims white space off the beginning. |
STRING_TRIM_TAIL | 0x2 | Trims white space off the end. |
STRING_TRIM | 0x3 | Trims white space off the beginning and end. |
Examples
Whenever you are accepting unstructured input from a user -- whether via chat or via a notecard -- it is a good idea to always full trim it:
llStringTrim("User input", STRING_TRIM);
This example returns the number of leading and trailing 'white space' characters on a string that were removed (not particularly useful but shows how to use the function).
default
{
state_entry()
{
llListen(4, "", llGetOwner(), "");
}
on_rez(integer start_param)
{
llResetScript();
}
listen(integer channel, string name, key id, string message)
{
//track the length
integer length = llStringLength(message);
//trim message (not necessary to store these to variables but makes reading easier)
string trim_left = llStringTrim(message, STRING_TRIM_HEAD);
string trim_right = llStringTrim(message, STRING_TRIM_TAIL);
string trim_both = llStringTrim(message, STRING_TRIM);
//output the results
llOwnerSay("Initial length = " + (string)length +
"\nLeading Spaces = " + (string)(length - llStringLength(trim_left))+
"\nTrailing Spaces = " + (string)(length - llStringLength(trim_right))+
"\nTrimmed Message = \"" + trim_both + "\"");
}
}
Notes
- The exact set of characters stripped are 0x09 (tab), line feed (0x0a, "\n"), vertical tab (0x0b), form feed (0x0c), carriage return (0x0d) and space (0x20, " ").
- Only line feed and space function as true whitespace within SL, the others will produce some manner of a printable character.
- The "\t" escape code doesn't actually represent the tab character, but several spaces instead.
- Other whitespace characters defined in Unicode, such as non-breaking space (0xa0) or specific-width spaces in the 0x2000 range are not stripped.
- Aside from white space at the beginning and / or end, the actual string will be unaffected. This means too, though, that extraneous spaces within the string -- for instance, a mistaken double-space type -- will not be corrected.
- The following will remove all doubled (or further multiplied), leading and trailing spaces with iterated llReplaceSubString:
while(llStringLength(s) != llStringLength(s = llReplaceSubString(s, " ", " ", 0)));
s = llStringTrim(s, STRING_TRIM);
- llReplaceSubString is also the easiest way to remove all spaces from a string:
llReplaceSubString("some words to remove the spaces from", " ", "", 0);
- llList2Json also trims strings contained in the list. Thus, an easy way to trim an entire list of strings is:
list l = [" a ", " b", "c ", " d "];
list trimmedList = llJson2List(llList2Json(JSON_ARRAY, l));
- Some old, less efficient methods, no longer recommended for use:
- Intra-string space trimming:
llDumpList2String(llParseString2List(src, [" "], []), " "); //works but can use a large quantity of memory
- An method to strip spaces from the sentence:
//Added By To-mos Codewarrior string str1 = "some words to remove the spaces from"; integer index; while(~index=llSubStringIndex(str1," ")) { data=llDeleteSubString(str1,index,index); }
- Variant of above:
(string)llParseString2List(src, [" "], []); //works but can use a large quantity of memory