String.indexOf() (Method)

The location of a sub-string within a string.

Availability:

ECMAScript edition - 2
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Netscape Enterprise server - 2.0
Opera - 3.0
Property/method value type:Number primitive
JavaScript syntax:-myString.indexOf(aSearchString)
-myString.indexOf(aSearchString, aPosition)
Argument list:aPositionAn optional valid character position within the string to indicate the search start
aSearchStringA string to search for within the String object

Returns a value indicating the location of the search string within the receiving String object.

If the search string is not found, the value -1 is returned.

You can indicate a starting position within the target string and if that argument is missing, the whole string will be searched. The same happens if you put the value 0 into the second parameter.

Example code:

   <HTML>
   <HEAD>
   </HEAD>
   <BODY>
   <SCRIPT>
   myString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   myIndex = myString.indexOf("MN");
   document.write(myString.substr(myIndex));
   </SCRIPT>
   </BODY>
   </HTML>

See also:String.prototype

Cross-references:

ECMA 262 edition 2 - section - 15.5.4.6

ECMA 262 edition 3 - section - 15.5.4.7