On Apr 25, 2010, at 5:00 AM, Jay Reeve wrote: > Is this what you want, or did I miss something? > > Jay > > local fn instr_new( startPos as long, haystack as Str255, needle as > Str255 ) > dim as long foundPos : foundPos = 0 > dim as long chrNum : chrNum = needle[ 0 ] > startPos -- > if startPos < 0 then startPos = 0 > > while startPos + chrNum <= hayStack[ 0 ] > > while needle[ chrNum ] == haystack[ startPos + chrNum ] > chrNum -- > if chrNum == 0 then foundPos = startPos + 1 : exit fn > wend > > chrNum = needle[0] > startPos ++ > wend > > end fn = foundPos Nice and clean, but is it The One!? :) My longer uglier version that I am still working on tested somewhat faster than yours in FB4 most times by up to 15 percent (for long strings), but not in FB5 where the optimized while loop must reign supreme over the for/next. Could your entry be optimized further (such is how I approached the situation) by not checking for any more needle where needle[1] is found but needle[last] is not also matched first? To see if you can skip any further iterating in between if there is no latter? Might not matter. Imagine the case where you have ( or have even longer strings to iterate) Fn Instr(1,"aaaaxXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" , "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXa") Can check for that little "a" out there at needle[max] upon first match of needle[1] and avoid further iteration? Not sure if it matters. In the next case, instr fn could check both beginning and end of needle and note a match, but the next position is bogus, so only one more iteration was going to be needed anyway. Fn Instr(1,"aaaaxXaXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" , "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") Whatever happened to your "Index all the unique words in the King James Bible code?" that took like 3 ticks? You updated that for FB5 yet? :) That optimizer in GCC might make it finish before it starts. :) rc