Difference between revisions of "LlSubStringIndex"

From Second Life Wiki
Jump to navigation Jump to search
(confirm our Efficiency Tester claims of equal run time with the much different Code Racer benchmark)
m (collect all our clear and conventional examples to look for one string inside another all together)
Line 35: Line 35:
</pre>
</pre>
|helpers=
|helpers=
Easy ways to see if a string exists in another string...
Tests to see if one string contains a copy of another:
 
1. Clear and conventional:


1. Correct at a glance and drearily conventional:
<pre>
<pre>
integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
Line 45: Line 46:
</pre>
</pre>


2. Smaller (~54 bytes rather than ~60):
<pre>
<pre>
integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
integer startswith(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
{
{
    return ~llSubStringIndex(haystack, needle);
return (0 == llSubStringIndex(haystack,needle));
}
}
</pre>
</pre>
Note: The llSubStringIndex function returns -1 only when not found and the ~ operator returns zero only for -1, so the arcane combination ~llSubStringIndex returns zero only for not found, else nonzero for found.
Note: Smaller was not noticeably faster when our [[Code Racer]] and [[Efficiency Tester]] harnesses measured the expression <nowiki>{ contains("wiki.secondlife.com", "wiki"); }</nowiki>.


<pre>
<pre>
integer startswith(string haystack, string needle)
integer endswith(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
{
{
return (0 == llSubStringIndex(haystack,needle));
return (llGetSubString(haystack,llStringLength(haystack)-llStringLength(needle),-1) == needle);
}
}
</pre>
</pre>


NOTE: This function doesn't use llSubStringIndex
Note: Some of the functions above calculate the answer without calling llSubStringIndex.
 
2. Smaller (tests contains in ~54 bytes rather than ~60):
 
<pre>
<pre>
integer endswith(string haystack, string needle)
integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
{
{
return (llGetSubString(haystack,llStringLength(haystack)-llStringLength(needle),-1) == needle);
    return ~llSubStringIndex(haystack, needle);
}
}
</pre>
</pre>
Note: The llSubStringIndex function returns -1 only when not found and the ~ operator returns zero only for -1, so the arcane combination ~llSubStringIndex returns zero only for not found, else nonzero for found.
Note: Smaller was Not noticeably faster when our [[Code Racer]] and [[Efficiency Tester]] harnesses measured the expression <nowiki>{ contains("wiki.secondlife.com", "wiki"); }</nowiki>.
|also_functions=
|also_functions=
{{LSL DefineRow||[[llListFindList]]|Find a list in another list}}
{{LSL DefineRow||[[llListFindList]]|Find a list in another list}}

Revision as of 17:04, 16 October 2007

Summary

Function: integer llSubStringIndex( string source, string pattern );

Returns an integer that is the index of pattern in source.

• string source
• string pattern

If pattern is not found in source, -1 is returned.
The index of the first character in the string is 0

Caveats

  • Performs a literal match (case sensitive).
    • Wildcards and RegEx are not supported.
  • Attempting to match an empty string ("") will return 0 instead of -1.
All Issues ~ Search JIRA for related Bugs

Examples

Matching against last names:

default
{
    state_entry()
    {
        llSensorRepeat("", NULL_KEY, AGENT, PI, 96.0, 20);
    }
    
    sensor(integer NumDet)
    {
        integer i;
        
        //Loop through all the sensor data and match against " Linden", 
        //this causes it to match with any last name of Linden (since there can't be spaces before the firstname)
        //Alternatively you could match a firstname with "FirstName "
        for(i = 0; i < NumDet; ++i)
            if(~llSubStringIndex(llDetectedName(i), " Linden"))
                llInstantMessage(llDetectedKey(i), "Hello, I see you!");
    }
}

Useful Snippets

Tests to see if one string contains a copy of another:

1. Clear and conventional:

integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
{
    return 0 <= llSubStringIndex(haystack, needle);
}
integer startswith(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
{
	return (0 == llSubStringIndex(haystack,needle));
}
integer endswith(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
{
	return (llGetSubString(haystack,llStringLength(haystack)-llStringLength(needle),-1) == needle);
}

Note: Some of the functions above calculate the answer without calling llSubStringIndex.

2. Smaller (tests contains in ~54 bytes rather than ~60):

integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex
{
    return ~llSubStringIndex(haystack, needle);
}

Note: The llSubStringIndex function returns -1 only when not found and the ~ operator returns zero only for -1, so the arcane combination ~llSubStringIndex returns zero only for not found, else nonzero for found.

Note: Smaller was Not noticeably faster when our Code Racer and Efficiency Tester harnesses measured the expression { contains("wiki.secondlife.com", "wiki"); }.

See Also

Functions

•  llListFindList Find a list in another list

Deep Notes

Search JIRA for related Issues

Signature

function integer llSubStringIndex( string source, string pattern );