Don't click here unless you want to be banned.

LSL Wiki : llSubStringIndex

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are
integer llSubStringIndex(string source, string pattern)

Returns the index in source where pattern first appears. Returns -1 if no match is found.

Remember, strings (and lists) are indexed starting from 0.

This function is relatively slow.

string sentence = "This sentence has a verb.";

llSay(0, (string)llSubStringIndex(sentence, "e")); // 6
llSay(0, (string)llSubStringIndex(sentence, "u")); // -1
llSay(0, (string)llSubStringIndex(sentence, "ten")); // 8

Q: "Relatively slow?" How exactly does this differ from the rest of LSL? :) -?
A: Actually, the execution of LSL functions themselves is quite fast, unless they have a built-in delay, as in the case of llInstantMessage, for example. The slow part (because of little allocation of timeslices because of the huge number of scripts a simulator runs in parallel) is the execution of the script's bytecode itself (loops, math, moving data in memory, etc.). So, unlike others functions, llSubStringIndex is likely simply slow in its execution, though it's possible that the slowness is due to an unknown built-in delay.
A2: In my own tests, this function performed 4.3 times slower than llGetSubString, completing 100 iterations in an average of 2.2 seconds on an open space sim versus 0.51 seconds. However, llDeleteSubString appears to perform even slower than this function, completing 100 iterations in an average of 2.3 seconds.

Compare with llListFindList.

This article wasn't helpful for you? Maybe the related article at the LSL Portal is able to bring enlightenment.

Functions | String
Comments [Hide comments/form]
There seems to be some sort of serious bug or deficiency in this function. Experimentation on my part shows that, paradoxically, the following code snippet performs better, both in speed and memory, than llSubStringIndex:

FindSubString(string source, string pattern){
list haystack = llParseString2List(source,[pattern],[]);
return -1;
} else {
return llStringLength(llList2String(hastack,0)) -1;
-- SchizzySapeur (2006-04-16 23:41:33)
I tried the above function with "This sentence no verb" as the source and "e" as the pattern. I think that taking 1 from the return value is wrong as it returns 6 without the subtraction.
-- HazVega (2006-04-17 14:24:29)
Also, if the pattern is the first thing in the string, its going to report the wrong information.
integer FindSubString(string source, string pattern) {
    list haystack = llParseString2List(source, [], [pattern]);
    string first = llList2String(haystack, 0);
    if (first == pattern) {
        return 0;
    } else if (llGetListLength(haystack) <= 1) {
        return -1;
    } else {
        return llStringLength(first);
Is that still faster?
-- ChristopherOmega (2006-04-18 13:59:14)
Tested in 1.10, the above comments about speed seem to be no longer true:

[22:16] Object: Ran 300 calls to llSubStringIndex in 9 seconds
[22:16] Object: Rate was 33.333332 calls/sec
[22:17] Object: Ran 300 calls to FindSubString in 29 seconds
[22:17] Object: Rate was 10.344828 calls/sec

This with a test that searched for a three character string in a variety of other strings - including where it was present zero, one, and many times.
-- ZarfVantongerloo (2006-06-08 22:20:35)
To COUNT substrings:
integer countsubstrings(string tstring, string tsubstring) { 
        return llGetListLength(llParseString2List(tstring,[],[tsubstring])) - llGetListLength(llParseString2List(tstring,[tsubstring],[])); 
-- PieroPadar (2006-06-09 01:57:36)
Eep, remember that MediaWiki syntax won't work here. You have to write [[Dog Dogs]] rather than [[Dog]]s if you want to link to a page with the plural form of the link text.
-- CatherineOmega (2006-06-10 03:52:59)
Um, no you don't, Catherine. [[string]] links to the same page as [[string]]s
-- EepQuirk (2006-06-11 00:00:02)
Attach a comment to this page: