Difference between revisions of "LlStringTrim"

From Second Life Wiki
Jump to navigation Jump to search
m
(Noted that llList2Json also trims strings)
(20 intermediate revisions by 12 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=
{{{!}}{{Prettytable}}
{{{!}} class="sortable" {{Prettytable|style=margin-top:0;}}
{{!}}-{{Hl2}}
{{!}}-{{Hl2}}
!colspan="2"{{!}}Constant
! {{!}} Constant
!Description
! title="Value" {{!}}
! class="unsortable"{{!}} Description
{{!}}-
{{!}}-
{{!}}[[STRING_TRIM_HEAD]]
{{!}}{{LSL Const|STRING_TRIM_HEAD|integer|1|hex=0x1|c=Trims white space off the beginning.}}
{{!}}{{LSL Hex|0x01|}}
{{!}}{{#var:value}}
{{!}}Trims spaces off the beginning.
{{!}}{{#var:comment}}
{{!}}-
{{!}}-
{{!}}[[STRING_TRIM_TAIL]]
{{!}}{{LSL Const|STRING_TRIM_TAIL|integer|2|hex=0x2|c=Trims white space off the end.}}
{{!}}{{LSL Hex|0x02|}}
{{!}}{{#var:value}}
{{!}}Trims spaces off the end
{{!}}{{#var:comment}}
{{!}}-
{{!}}-
{{!}}[[STRING_TRIM]]
{{!}}{{LSL Const|STRING_TRIM|integer|3|hex=0x3|c=Trims white space off the beginning and end.}}
{{!}}{{LSL Hex|0x03|}}
{{!}}{{#var:value}}
{{!}}Trims spaces off the beginning and end.
{{!}}{{#var:comment}}
{{!}}}
{{!}}}
|examples=<pre>
|examples=
integer channel;                        // will be set by notecard
string NOTECARD_NAME = "configuration";  // name of notecard goes here


key KQuery;                  // to seperate Dataserver requests
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:


// UPDATES
<source lang="lsl2">llStringTrim("User input", STRING_TRIM);</source>
integer line;


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).
<source lang="lsl2">
default
default
{
{
     changed(integer change)        
     state_entry()
     {
     {
         // We want to reload channel notecard if it changed
         llListen(4, "", llGetOwner(), "");
        if (change & CHANGED_INVENTORY)
        {
        llResetScript();
        }
       
     }
     }
     state_entry()
 
     on_rez(integer start_param)
     {
     {
         KQuery = llGetNotecardLine(NOTECARD_NAME, line);// request line
         llResetScript();
        line++ ;
     }
     }
   
    dataserver(key query_id, string data) {
        if (query_id == KQuery) {
            if (data != EOF) {    // not at the end of the notecard
                // yay!  Parsing time
                // first, is it a comment? or an empty line?
                if (llGetSubString (data, 0, 0) != "#" && llStringLength (data) > 0)
                {
                    list parsed = llParseString2List (data, ["="], [""]) ;
                    string token = llToLower (llStringTrim (llList2String (parsed, 0), STRING_TRIM)) ;
                    if (token == "command_channel")
                        channel = (integer)llStringTrim (llList2String (parsed, 1), STRING_TRIM) ;
                }


                KQuery = llGetNotecardLine(NOTECARD_NAME, line);
    listen(integer channel, string name, key id, string message)
                line++;
    {     
            } else {
        //track the length
                llOwnerSay ("Done Reading Notecard: " + (string)NOTECARD_NAME) ;
        integer length = llStringLength(message);
state configuration ;
       
            }
        //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 + "\"");
    }
}
}
 
</source>
state configuration
{
            state_entry()
            {
            llListen(channel, "", "", "");
            llShout(0, "Channel set to " + (string)channel);
            } 
}
</pre>
Submitted by [https://wiki.secondlife.com/wiki/User:Revolution_Perenti Revolution Perenti]
|helpers
|helpers
|also_functions
|also_functions
Line 95: Line 74:
|also_events
|also_events
|also_articles
|also_articles
|notes=This function is available in 1.13.4 onwards...
|history=
*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=
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.
<source lang="lsl2">llDumpList2String(llParseString2List(src, [" "], []), " "); //works but can use a large quantity of memory</source>
 
This method strips spaces from the sentence and may use less memory
<source 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);
}
</source>
The following will do the same:
<source lang="lsl2">(string)llParseString2List(src, [" "], []); //works but can use a large quantity of memory</source>
 
[[llList2Json]] also trims strings contained in the list. Thus, an easy way to trim an entire list of strings is:
<source lang="lsl2">
list l = ["  a  ", " b", "c ", " d "];
list trimmedList = llJson2List(llList2Json(JSON_ARRAY, l));
</source>


The following examples will make the same result.
<pre>
string src = "  Dah    ";
st = llStringTrim(src, STRING_TRIM_HEAD);
st = llStringTrim(st , STRING_TRIM_TAIL);
</pre>
<pre>
string src = "  Dah    ";
st = llStringTrim(src, STRING_TRIM);
</pre>


This is because <code>(STRING_TRIM_HEAD {{!}} STRING_TRIM_TAIL) == STRING_TRIM</code>.
|deprecated
|deprecated
|cat1=String
|cat1=String

Revision as of 18:11, 20 April 2017

Summary

Function: string llStringTrim( string src, integer type );

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

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));

Deep Notes

History

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

Search JIRA for related Issues

Signature

function string llStringTrim( string src, integer type );