Difference between revisions of "LlStringTrim"

From Second Life Wiki
Jump to navigation Jump to search
m
m (typo)
 
(16 intermediate revisions by 13 users not shown)
Line 8: Line 8:
|func_footnote
|func_footnote
|func_desc
|func_desc
|return_text=that is '''src''' with leading and/or trailing white space (spaces, tabs, and line feeds) trimmed from it.
|return_text=that is {{LSLP|src}} with leading and/or trailing white space (spaces, tabs, and line feeds) trimmed from it.
|spec
|spec
|caveats
|caveats
|constants=
|constants=
{{{!}} class="sortable" {{Prettytable}}
{{{!}} class="sortable" {{Prettytable|style=margin-top:0;}}
{{!}}-{{Hl2}}
{{!}}-{{Hl2}}
! {{!}} Constant
! {{!}} Constant
Line 18: Line 18:
! class="unsortable"{{!}} Description
! class="unsortable"{{!}} Description
{{!}}-
{{!}}-
{{!}}{{LSL Const|STRING_TRIM_HEAD|integer|1|hex=0x1|c=Trims spaces off the beginning.}}
{{!}}{{LSL Const|STRING_TRIM_HEAD|integer|1|hex=0x1|c=Trims white space off the beginning.}}
{{!}}{{#var:value}}
{{!}}{{#var:value}}
{{!}}{{#var:comment}}
{{!}}{{#var:comment}}
{{!}}-
{{!}}-
{{!}}{{LSL Const|STRING_TRIM_TAIL|integer|2|hex=0x2|c=Trims spaces off the end.}}
{{!}}{{LSL Const|STRING_TRIM_TAIL|integer|2|hex=0x2|c=Trims white space off the end.}}
{{!}}{{#var:value}}
{{!}}{{#var:value}}
{{!}}{{#var:comment}}
{{!}}{{#var:comment}}
{{!}}-
{{!}}-
{{!}}{{LSL Const|STRING_TRIM|integer|3|hex=0x3|c=Trims spaces off the beginning and end.}}
{{!}}{{LSL Const|STRING_TRIM|integer|3|hex=0x3|c=Trims white space off the beginning and end.}}
{{!}}{{#var:value}}
{{!}}{{#var:value}}
{{!}}{{#var:comment}}
{{!}}{{#var:comment}}
{{!}}}
{{!}}}
|examples=
|examples=
Note that aside from spaces at the beginning and / or end, the actually 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. Sadly, there is no general "Trim" function in LSL that will deal with these as well.
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>


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).
 
<syntaxhighlight lang="lsl2">
 
This example returns the number of leading and trailing spaces on a string (not particularly useful but shows how to use the function).
<lsl>
default
default
{
{
Line 47: Line 42:
         llListen(4, "", llGetOwner(), "");
         llListen(4, "", llGetOwner(), "");
     }
     }
     on_rez(integer a)
 
     on_rez(integer start_param)
     {
     {
         llResetScript();
         llResetScript();
     }
     }
     listen(integer chan, string name, key id, string msg)
 
     {
     listen(integer channel, string name, key id, string message)
        //test for and remove wrapping single or double quotes
     {      
        if(~llSubStringIndex("'\"", llGetSubString(msg,0,0)))
            if(llGetSubString(msg,-1,-1) == llGetSubString(msg,0,0))
                msg = llDeleteSubString(msg, -1, 0);
       
         //track the length
         //track the length
         integer length = llStringLength(msg);
         integer length = llStringLength(message);
          
          
         //trim msg (not necessary to store these to variables but makes reading easier)
         //trim message (not necessary to store these to variables but makes reading easier)
         string trim_left = llStringTrim(msg, STRING_TRIM_HEAD);
         string trim_left = llStringTrim(message, STRING_TRIM_HEAD);
         string trim_right = llStringTrim(msg, STRING_TRIM_HEAD);
         string trim_right = llStringTrim(message, STRING_TRIM_TAIL);
         string trim = llStringTrim(msg, STRING_TRIM);
         string trim_both  = llStringTrim(message, STRING_TRIM);


         //output the results
         //output the results
         llOwnerSay("Initial length = " + (string)length +
         llOwnerSay("Initial length = " + (string)length +
                 "\nLeading Spaces = " + (string)(length - llStringLength(trim_left))+
                 "\nLeading Spaces = " + (string)(length - llStringLength(trim_left))+
                 "\nTrailing Spaces = " + (string)(length - llStringLength(trim_right))+
                 "\nTrailing Spaces = " + (string)(length - llStringLength(trim_right))+
                 "\nTrimmed Message = \"" + trim + "\"");
                 "\nTrimmed Message = \"" + trim_both + "\"");
     }
     }
}
}
</lsl>
</syntaxhighlight>
|helpers
|helpers
|also_functions
|also_functions
Line 81: Line 73:
|history=
|history=
*Introduced in 1.13.4
*Introduced in 1.13.4
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=
The following examples will make the same result.
*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, " ").
<lsl>
** Only line feed and space function as true whitespace within SL, the others will produce some manner of a printable character.
string src = "   Duh    ";
** The "\t" escape code doesn't actually represent the tab character, but several spaces instead.
st = llStringTrim(src, STRING_TRIM_HEAD);
** Other whitespace characters defined in Unicode, such as non-breaking space (0xa0) or specific-width spaces in the 0x2000 range are not stripped.
st = llStringTrim(st , STRING_TRIM_TAIL);
* 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.
</lsl>
* The following will remove all double spaces and leading and trailing spaces.
<lsl>
<syntaxhighlight lang="lsl2">llDumpList2String(llParseString2List(src, [" "], []), " "); //works but can use a large quantity of memory</syntaxhighlight>
string src = "   Duh    ";
 
st = llStringTrim(src, STRING_TRIM);
* This method strips spaces from the sentence and may use less memory
</lsl>
<syntaxhighlight lang="lsl2">
//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);
}
</syntaxhighlight>
* The following will do the same:
<syntaxhighlight lang="lsl2">(string)llParseString2List(src, [" "], []); //works but can use a large quantity of memory</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 = [" ", " b", "c ", " d "];
list trimmedList = llJson2List(llList2Json(JSON_ARRAY, l));
</syntaxhighlight>


This is because <code>(STRING_TRIM_HEAD {{!}} STRING_TRIM_TAIL) == STRING_TRIM</code>.
* Finally, [[llReplaceSubString]] is probably the easiest way to trim spaces from a string:
<syntaxhighlight lang="lsl2">llReplaceSubString("some words to remove the spaces from", " ", "", 0);</syntaxhighlight>
|deprecated
|deprecated
|cat1=String
|cat1=String

Latest revision as of 12:04, 21 May 2024

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 double spaces and leading and trailing spaces.
llDumpList2String(llParseString2List(src, [" "], []), " "); //works but can use a large quantity of memory
  • This method strips spaces from the sentence and may use less memory
//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);
}
  • The following will do the same:
(string)llParseString2List(src, [" "], []); //works but can use a large quantity of memory
  • 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));
llReplaceSubString("some words to remove the spaces from", " ", "", 0);

Deep Notes

History

  • Introduced in 1.13.4
Date of release 25-04-2007

Signature

function string llStringTrim( string src, integer type );