String.indexOf()

var words:String = "one two three four five";

trace("two: ", words.indexOf("two"));

var letters:String = "abcd";

trace("d: ",letters.indexOf("d"));

trace("z: ",letters.indexOf("z"));

/*
outputs:
two:  4
d:  3
z:  -1
*/

indexOf() searches a string for another string and returns an index… in line 3 above, I search the words string for the smaller string “two” and indexOf() gives me the index of the letter “t”. If indexOf() doesn’t find anything it will return -1 (as in the case of line 9).

I seem to recall using this in some unexpected places. I’ll see if I can dig up an example over the next few days.

Leave a Reply

Your email address will not be published. Required fields are marked *