Difference between revisions of "LlStringTrim"

From Second Life Wiki
Jump to: navigation, search
m (code > lsl)
Line 97: Line 97:
|haiku={{Haiku|Less around my ears|And off the collar this time.|Just not too short, please.}}

Revision as of 07:38, 12 April 2014


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.


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:

<lsl>llStringTrim("User input", STRING_TRIM);</lsl>

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 {

       llListen(4, "", llGetOwner(), "");
   on_rez(integer start_param)
   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 + "\"");




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>

This method strips spaces from the sentence and may use less memory <lsl> //Added By To-mos Codewarrior string str1 = "some words to remove the spaces from"; integer index; while(~index=llSubStringIndex(str1," ")) {


} </lsl> The following will do the same: <lsl>(string)llParseString2List(src, [" "], []); //works but can use a large quantity of memory</lsl>

Deep Notes


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

Search JIRA for related Issues


function string llStringTrim( string src, integer type );


Less around my ears
And off the collar this time.
Just not too short, please.