Stristr: Difference between revisions
Jump to navigation
Jump to search
Streamlining, we test haystack so we can use negative indexes; the before_needle syntax seemed backwards: false is less than true, so false should describe what comes earlier in the string. |
mNo edit summary |
||
| 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=after_needle|p3_desc=If TRUE, '''Stristr''' returns the part of the '''haystack''' after the first occurrence of the '''needle''', otherwise before 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=containing the text from '''haystack''' that comes before or after '''needle'''. | |return_text=containing the text from '''haystack''' that comes before or after '''needle'''. | ||
Revision as of 23:00, 30 May 2010
| LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
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
<lsl>//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 "";
} </lsl>
Examples
This example uses its basic feature, to provide the content left, or right of the searched string. <lsl>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, "
}
}</lsl>
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 |