Difference between revisions of "LlStringTrim"

From Second Life Wiki
Jump to navigation Jump to search
m (actually -> actual)
m (refer to "white space" consistently. Remove confusing extras. Rigorous alignment helps with analysis of the code.)
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}}
Line 37: Line 37:




This example returns the number of leading and trailing spaces on a string (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).
<lsl>
<lsl>
default
default
Line 50: Line 50:
     }
     }
     listen(integer chan, string name, key id, string msg)
     listen(integer chan, string name, key id, string msg)
     {
     {      
        //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(msg);
          
          
         //trim msg (not necessary to store these to variables but makes reading easier)
         //trim msg (not necessary to store these to variables but makes reading easier)
         string trim_left = llStringTrim(msg, STRING_TRIM_HEAD);
         string trim_left = llStringTrim(msg, STRING_TRIM_HEAD);
         string trim_right = llStringTrim(msg, STRING_TRIM_TAIL);
         string trim_right = llStringTrim(msg, STRING_TRIM_TAIL);
         string trim = llStringTrim(msg, STRING_TRIM);
         string trim_both  = llStringTrim(msg, 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 + "\"");
     }
     }
}
}
Line 80: Line 75:
*Introduced in 1.13.4
*Introduced in 1.13.4
|notes=
|notes=
Aside from spaces 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.  
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.
The following will remove all double spaces and leading and trailing spaces.

Revision as of 13:45, 15 January 2013

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). <lsl> default {

   state_entry()
   {
       llListen(4, "", llGetOwner(), "");
   }
   on_rez(integer a)
   {
       llResetScript();
   }
   listen(integer chan, string name, key id, string msg)
   {       
       //track the length
       integer length = llStringLength(msg);
       
       //trim msg (not necessary to store these to variables but makes reading easier)
       string trim_left  = llStringTrim(msg, STRING_TRIM_HEAD);
       string trim_right = llStringTrim(msg, STRING_TRIM_TAIL);
       string trim_both  = llStringTrim(msg, 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 + "\"");
   }

}

</lsl>

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. <lsl>llDumpList2String(llParseString2List(src, [" "], []), " "); //works but can use a large quantity of memory</lsl>

Deep Notes

History

  • Introduced in 1.13.4

Signature

function string llStringTrim( string src, integer type );