User Guide
String.indexOf() 799
String.indexOf()
Availability
Flash Player 5.
Usage
my_str.indexOf(substring:String, [startIndex:Number])
Parameters
substring
A string; the substring to be searched for within my_str.
startIndex A number; an optional integer specifying the starting point in my_str to search for
the substring.
Returns
A number; the position of the first occurrence of the specified substring or -1.
Description
Method; searches the string and returns the position of the first occurrence of substring
found at or after
startIndex within the calling string. This index is zero-based, meaning that the
first character in a string is considered to be at index 0—not index 1. If
substring is not found,
the method returns -1.
Example
The following examples use indexOf() to return the index of characters and substrings:
var searchString:String = "Lorem ipsum dolor sit amet.";
var index:Number;
index = searchString.indexOf("L");
trace(index); // output: 0
index = searchString.indexOf("l");
trace(index); // output: 14
index = searchString.indexOf("i");
trace(index); // output: 6
index = searchString.indexOf("ipsum");
trace(index); // output: 6
index = searchString.indexOf("i", 7);
trace(index); // output: 19
index = searchString.indexOf("z");
trace(index); // output: -1
See also
String.lastIndexOf()