1. var words:String = "one two three four five";
  3. trace("two: ", words.indexOf("two"));
  5. var letters:String = "abcd";
  7. trace("d: ",letters.indexOf("d"));
  9. trace("z: ",letters.indexOf("z"));
  11. /*
  12. outputs:
  13. two:  4
  14. d:  3
  15. z:  -1
  16. */

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.

This entry was posted in string manipulation, strings and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *