Difference between revisions of "Stristr"

From Second Life Wiki
Jump to navigation Jump to search
(Created page with '{{LSL_Function |func=Stristr |mode=user |p1_type=string|p1_name=haystack|p1_desc=The input string |p2_type=string|p2_name=needle|p2_desc=the search within |p3_type=integer|p3_nam...')
 
m (<lsl> tag to <source>)
 
(2 intermediate revisions by one other user not shown)
Line 4: Line 4:
|p1_type=string|p1_name=haystack|p1_desc=The input string
|p1_type=string|p1_name=haystack|p1_desc=The input string
|p2_type=string|p2_name=needle|p2_desc=the search within
|p2_type=string|p2_name=needle|p2_desc=the search within
|p3_type=integer|p3_name=before_needle|p3_desc=If TRUE, '''Stristr''' returns the part of the '''haystack''' before the first occurrence of the '''needle''', else after first occurrence.
|p3_type=integer|p3_name=after_needle|p3_desc=If [[TRUE]], '''Stristr''' returns the part of the '''haystack''' after the first occurrence of the '''needle''', otherwise before first occurrence.
|return_type=string
|return_type=string
|return_text=from the first occurrence of '''needle''' to the end of '''haystack'''.
|return_text=containing the text from '''haystack''' that comes before or after '''needle'''.
|func_desc=Returns part of '''haystack''' string from the first occurrence of '''needle''' to the end of '''haystack'''.  
|func_desc=Returns part of '''haystack''' string from the first occurrence of '''needle''' to the end of '''haystack'''.  
|func_footnote=This function can also be used to determine if a line is in a string, If needle is not found it will return with a null string for [[if else]] statements.
|func_footnote
|spec=<lsl>
|spec=<source lang="lsl2">//Function created by Ugleh Ulrik
//Function created by Ugleh Ulrik
string stristr(string haystack, string needle, integer after_needle){
string stristr(string haystack, string needle, integer before_needle){
    if(haystack)
        integer msl = llStringLength(haystack);
    {//we have a haystack, it's not ""
        integer mi;
         //Now find us a needle!
        list mlist;
         integer pos = llSubStringIndex(haystack, needle);
        do//we make a list out of the message to use ListFindList
         if(~pos)
        mlist += [llGetSubString(haystack, mi, mi)];
         {//We have found a needle!
         while(msl>++mi);
            if(after_needle)//return what comes after needle
         integer ssl = llStringLength(needle);
                return llGetSubString(haystack, pos + llStringLength(needle), -1);
         integer si;
            return llDeleteSubString(haystack, pos, -1);
         list slist;
         }
        do//we make a list out of the search to use ListFindList
    }
        slist += [llGetSubString(needle, si, si)];
    return "";
        while(ssl>++si);
        if (llListFindList(mlist,slist) == -1)//-1 means not found, so we return it as null
        return "";
        if(before_needle)
        return llGetSubString(haystack, msl, ((integer)llListFindList(mlist,slist) - 1));
        return llGetSubString(haystack, ((integer)llListFindList(mlist,slist) + ssl), msl);
          
}
}
</lsl>
</source>
|examples=
|examples=
This example uses its basic feature, to provide the content left, or right of the searched string.
This example uses its basic feature, to provide the content left, or right of the searched string.
<lsl>
<source lang="lsl2">default
default
{
{
     touch_start(integer total_number)
     touch_start(integer total_number)
     {
     {
        string source = "Hello there, Ugleh is my name!";
        string source = "Hello there, Ugleh is my name!";
         string mess = stristr(source,"Ugleh",FALSE); //False means after Needle
         string mess = stristr(source,"Ugleh",TRUE); //True means after Needle
         llOwnerSay(mess);//outputs  "  is my name!"
         llOwnerSay(mess);//outputs  "  is my name!"
          
          
         mess = stristr(source,"Ugleh",TRUE); //True means before Needle.
         mess = stristr(source,"Ugleh",FALSE); //False means before Needle.
         llOwnerSay(mess);//outputs "Hello there, "
         llOwnerSay(mess);//outputs "Hello there, "
       
    }
}
</lsl>
This uses the function to provide an if else statement
<lsl>
default
{
    touch_start(integer total_number)
    {
        string source = "Ugleh was here";//change to "Strife was here" to get a different result
        string mess = stristr(source,"Ugleh",FALSE);
        llOwnerSay(mess);//outputs  "  is my name!"
       
        mess = stristr(source,"Strife",TRUE);
        if(mess)
        llSay(0, "Looks like Strife was here");
        else
        llSay(0, "Looks like Ugleh was here");
       
       
     }
     }
}
}</source>
 
</lsl>
|helpers
|helpers
|notes
|notes
|also
|also
|also_functions
|also_functions=
{{LSL DefineRow||[[llSubStringIndex]]|Find a string in another string}}
{{LSL DefineRow||[[llParseString2List]]|Split a string up into a list}}
{{LSL DefineRow||[[llParseStringKeepNulls]]|Like llParseString2List but it keeps empty strings}}
{{LSL DefineRow||[[llListFindList]]|Like [[llSubStringIndex]] but for lists}}
|also_articles
|also_articles
|cat1=Examples
|cat1=Examples

Latest revision as of 15:39, 22 January 2015

Summary

Function: string Stristr( string haystack, string needle, integer after_needle );

Returns part of haystack string from the first occurrence of needle to the end of haystack.
Returns a string containing the text from haystack that comes before or after needle.

• string haystack The input string
• string needle the search within
• integer after_needle If TRUE, Stristr returns the part of the haystack after the first occurrence of the needle, otherwise before first occurrence.

Specification

//Function created by Ugleh Ulrik
string stristr(string haystack, string needle, integer after_needle){
    if(haystack)
    {//we have a haystack, it's not ""
        //Now find us a needle!
        integer pos = llSubStringIndex(haystack, needle);
        if(~pos)
        {//We have found a needle!
            if(after_needle)//return what comes after needle
                return llGetSubString(haystack, pos + llStringLength(needle), -1);
            return llDeleteSubString(haystack, pos, -1);
        }
    }
    return "";
}

Examples

This example uses its basic feature, to provide the content left, or right of the searched string.

default
{
    touch_start(integer total_number)
    {
         string source = "Hello there, Ugleh is my name!";
         string mess = stristr(source,"Ugleh",TRUE); //True means after Needle
         llOwnerSay(mess);//outputs  "  is my name!"
         
         mess = stristr(source,"Ugleh",FALSE); //False means before Needle.
         llOwnerSay(mess);//outputs "Hello there, "
    }
}

See Also

Functions

•  llSubStringIndex Find a string in another string
•  llParseString2List Split a string up into a list
•  llParseStringKeepNulls Like llParseString2List but it keeps empty strings
•  llListFindList Like llSubStringIndex but for lists