JavaScript indexOf String Function

Updated on 20 Mar 2012,
Published on 11 Mar 2008

In JavaScript String Object has a function indexOf that is most often used to get the position of specified string pattern passed in the indexOf function. JavaScript indexOf function returns the index position for the first occurrence of specified string pattern in the provided string. By default IndexOf function starts searching from 0th index of the string. If you will pass the optional parameter of start index then it starts searching the string from the specified index.

JavaScript String Object Examples:

You can see the live samples and examples of JavaScript String Object from the following links:

You can use the sample example string that we used in the previous article about JavaScript String Functions-1:

var myString = new String();

myString = "JavaScript String Function";

myString variable is assigned a string to perform the string functions:

JavaScript IndexOf String Function Syntax

stringObject.indexOf([string pattern], [OPTIONAL start index]);

Exmaple of JavaScript IndexOf Function without optional parameter

document.write( myString.indexOf("ri") );

Above example string will return the index of first occurrence of ri pattern in the provided string.

Exmaple of JavaScript IndexOf Function with startIndex value passed

document.write( myString.indexOf("ri",7) );

Output:

You can see the output of above discussed code from the following links:

IndexOf String Function 01

IndexOf String Function 02

Continue to next tutorial: Javascript String LastIndexOf Function to learn how to get the index of specified string pattern that occurs nearest to the last index of the provided string.

0 Responses to "JavaScript indexOf String Function"
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email
  • HIRE EzineASP.Net Developers