Difference between revisions of "LlStringTrim"

From Second Life Wiki
Jump to: navigation, search
m
(Undo revision 30041 by Revolution Perenti That example is better for notecard reading then how to use llStringTrim, it's overkill)
Line 12: Line 12:
 
|caveats
 
|caveats
 
|constants=
 
|constants=
{{{!}}{{Prettytable}}
+
{{{!}} class="sortable" {{Prettytable}}
 
{{!}}-{{Hl2}}
 
{{!}}-{{Hl2}}
!colspan="2"{{!}}Constant
+
!{{!}}Constant
!Description
+
! title="Value" {{!}}
 +
! Description
 
{{!}}-
 
{{!}}-
{{!}}[[STRING_TRIM_HEAD]]
+
{{!}}{{LSL Const|STRING_TRIM_HEAD|integer|1|hex=0x1|c=Trims spaces 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 spaces 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 spaces 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
+
Returns the number of leading and trailing spaces on a string (not particularly useful but shows how to use the function).
string NOTECARD_NAME = "configuration";  // name of notecard goes here
+
<pre>
 
+
key KQuery;                  // to seperate Dataserver requests
+
 
+
// UPDATES
+
integer line;
+
 
+
 
default
 
default
 
{
 
{
     changed(integer change)         
+
     state_entry()
 +
    {
 +
         llListen(4, "", llGetOwner(), "");
 +
    }
 +
    on_rez(integer a)
 
     {
 
     {
        // We want to reload channel notecard if it changed
 
        if (change & CHANGED_INVENTORY)
 
        {
 
 
         llResetScript();
 
         llResetScript();
        }
 
       
 
 
     }
 
     }
     state_entry()
+
     listen(integer chan, string name, key id, string msg)
 
     {
 
     {
         KQuery = llGetNotecardLine(NOTECARD_NAME, line);// request line
+
         //test for and remove wrapping single or double quotes
         line++ ;
+
        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(src, STRING_TRIM_HEAD);
 +
        string trim_right = llStringTrim(src, STRING_TRIM_HEAD);
 +
        string trim = llStringTrim(src, 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 + "\"");
 
     }
 
     }
   
 
    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);
 
                line++;
 
            } else {
 
                llOwnerSay ("Done Reading Notecard: " + (string)NOTECARD_NAME) ;
 
state configuration ;
 
            }
 
        }
 
    } 
 
 
}
 
 
state configuration
 
{
 
 
            state_entry()
 
            {
 
            llListen(channel, "", "", "");
 
            llShout(0, "Channel set to " + (string)channel);
 
            } 
 
 
}
 
}
 
</pre>
 
</pre>
Submitted by [https://wiki.secondlife.com/wiki/User:Revolution_Perenti Revolution Perenti]
 
 
|helpers
 
|helpers
 
|also_functions
 
|also_functions
Line 95: Line 71:
 
|also_events
 
|also_events
 
|also_articles
 
|also_articles
|notes=This function is available in 1.13.4 onwards...
+
|history
 
+
*Introduced in 1.13.4 onwards...
 +
|notes=
 
The following examples will make the same result.
 
The following examples will make the same result.
 
<pre>
 
<pre>
string src = "  Dah   ";
+
string src = "  Duh   ";
 
st = llStringTrim(src, STRING_TRIM_HEAD);
 
st = llStringTrim(src, STRING_TRIM_HEAD);
 
st = llStringTrim(st , STRING_TRIM_TAIL);
 
st = llStringTrim(st , STRING_TRIM_TAIL);
 
</pre>
 
</pre>
 
<pre>
 
<pre>
string src = "  Dah   ";
+
string src = "  Duh   ";
 
st = llStringTrim(src, STRING_TRIM);
 
st = llStringTrim(src, STRING_TRIM);
 
</pre>
 
</pre>

Revision as of 11:14, 1 September 2007

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

Returns the number of leading and trailing spaces on a string (not particularly useful but shows how to use the function).

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(src, STRING_TRIM_HEAD);
        string trim_right = llStringTrim(src, STRING_TRIM_HEAD);
        string trim = llStringTrim(src, 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 + "\"");
    }
}

Notes

The following examples will make the same result.

string src = "   Duh    ";
st = llStringTrim(src, STRING_TRIM_HEAD);
st = llStringTrim(st , STRING_TRIM_TAIL);
string src = "   Duh    ";
st = llStringTrim(src, STRING_TRIM);

This is because (STRING_TRIM_HEAD | STRING_TRIM_TAIL) == STRING_TRIM.

Deep Notes

Search JIRA for related Issues

Signature

function string llStringTrim( string src, integer type );