Difference between revisions of "LlStringTrim"

From Second Life Wiki
Jump to: navigation, search
m
Line 12: Line 12:
 
|caveats
 
|caveats
 
|constants=
 
|constants=
{{{!}} class="sortable" {{Prettytable}}
+
{{{!}} class="sortable" {{Prettytable|style=margin-top:0;}}
 
{{!}}-{{Hl2}}
 
{{!}}-{{Hl2}}
 
! {{!}} Constant
 
! {{!}} Constant
Line 31: Line 31:
 
{{!}}}
 
{{!}}}
 
|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:
Line 82: Line 80:
 
*Introduced in 1.13.4
 
*Introduced in 1.13.4
 
|notes=
 
|notes=
The following examples will make the same result.
+
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.
<lsl>
+
string src = "  Duh    ";
+
st = llStringTrim(src, STRING_TRIM_HEAD);
+
st = llStringTrim(st , STRING_TRIM_TAIL);
+
</lsl>
+
<lsl>
+
string src = "  Duh    ";
+
st = llStringTrim(src, STRING_TRIM);
+
</lsl>
+
  
This is because <code>(STRING_TRIM_HEAD {{!}} STRING_TRIM_TAIL) == STRING_TRIM</code>.
+
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>
 
|deprecated
 
|deprecated
 
|cat1=String
 
|cat1=String

Revision as of 06:33, 8 July 2008

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 spaces off the beginning.
STRING_TRIM_TAIL 0x2 Trims spaces off the end.
STRING_TRIM 0x3 Trims spaces 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 spaces on a string (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)
   {
       //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
       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_HEAD);
       string trim = 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 + "\"");
   }

}

</lsl>

Notes

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.

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

Search JIRA for related Issues

Signature

function string llStringTrim( string src, integer type );