Difference between revisions of "LlSubStringIndex"

From Second Life Wiki
Jump to navigation Jump to search
m (lsl code tagging)
Line 5: Line 5:
|func_footnote=If '''pattern''' is not found in '''source''', {{HoverText|-1|negative one, 0x{{LSL_Hex/Write|-1}}}} is returned.<br/>
|func_footnote=If '''pattern''' is not found in '''source''', {{HoverText|-1|negative one, 0x{{LSL_Hex/Write|-1}}}} is returned.<br/>
The index of the first character in the string is {{HoverText|0|zero}}
The index of the first character in the string is {{HoverText|0|zero}}
You cannot search backwards using a negative number, unless you use the user-created function supplied below.
|func_desc
|func_desc
|return_text=that is the index of '''pattern''' in '''source'''.
|return_text=that is the index of '''pattern''' in '''source'''.
Line 12: Line 14:
*Attempting to match an empty string ("") will return 0 instead of -1.
*Attempting to match an empty string ("") will return 0 instead of -1.
|constants
|constants
|examples=Matching against last names:
|examples=
 
Note: some people maintain that this function is relatively slow compared to other LSL functions. They are not certain if it is because of execution time, or some built-in delay.
 
Matching against last names:
<lsl>
<lsl>
default
default
Line 74: Line 80:


Note: Smaller was not noticeably faster or slower when our [[Code Racer]] and [[Efficiency Tester]] harnesses measured the expression <nowiki>{ contains("wiki.secondlife.com", "wiki"); }</nowiki>.
Note: Smaller was not noticeably faster or slower when our [[Code Racer]] and [[Efficiency Tester]] harnesses measured the expression <nowiki>{ contains("wiki.secondlife.com", "wiki"); }</nowiki>.
3. '''Function to search backwards'''
<lsl>
integer llSubStringIndexRev(string src, string pattern, integer start) {
  string StrPart;
    integer Pos = start + 1;
   
    do{
        Pos = Pos - 1;
        StrPart = llGetSubString(src, Pos, Pos + llStringLength(pattern) - 1);
    }
    while (Pos > -1 && StrPart != pattern);
      return Pos;
}
</lsl>
(Source: retrieved from http://rpgstats.com/wiki/index.php?title=LlSubStringIndex July 2008, with slight modifications to reduce text.
|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 21:23, 7 July 2008

Summary

Function: integer llSubStringIndex( string source, string pattern );
0.0 Forced Delay
10.0 Energy

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

You cannot search backwards using a negative number, unless you use the user-created function supplied below.

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.

Examples

Note: some people maintain that this function is relatively slow compared to other LSL functions. They are not certain if it is because of execution time, or some built-in delay.

Matching against last names: <lsl> 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!");
   }

}

</lsl>

Useful Snippets

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

1. Concise & conventional:

<lsl> integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex {

   return 0 <= llSubStringIndex(haystack, needle);

} </lsl>

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

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

Note: Some of the snippets above return a result without ever calling llSubStringIndex.

2. Clever & smaller (calculates contains in ~54 bytes rather than ~60):

<lsl> integer contains(string haystack, string needle) // http://wiki.secondlife.com/wiki/llSubStringIndex {

   return ~llSubStringIndex(haystack, needle);

} </lsl>

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

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

3. Function to search backwards

<lsl> integer llSubStringIndexRev(string src, string pattern, integer start) {

 string StrPart;
   integer Pos = start + 1;
   
   do{
       Pos = Pos - 1;
       StrPart = llGetSubString(src, Pos, Pos + llStringLength(pattern) - 1);
   }
   while (Pos > -1 && StrPart != pattern);
     return Pos;

} </lsl>

(Source: retrieved from http://rpgstats.com/wiki/index.php?title=LlSubStringIndex July 2008, with slight modifications to reduce text.

See Also

Functions

•  llListFindList Find a list in another list
•  llGetSubString Copy out part of a string of characters

Deep Notes

Signature

function integer llSubStringIndex( string source, string pattern );