String.prototype.indexOf()

  • Revision slug: JavaScript/Reference/Global_Objects/String/indexOf
  • Revision title: indexOf
  • Revision id: 62467
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment 3 words added, 5 words removed

Revision Content

 

Summary

Returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex,
returns -1 if the value is not found.

Method of String
Implemented in JavaScript 1.0
ECMAScript Edition ECMAScript 1st Edition

Syntax

string.indexOf(searchValue[, fromIndex])

Parameters

searchValue 
A string representing the value to search for.
fromIndex 
The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.

Description

Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character of a string called stringName is stringName.length - 1.

"Blue Whale".indexOf("Blue")    // returns 0
"Blue Whale".indexOf("Blute")   // returns -1
"Blue Whale".indexOf("Whale",0) // returns 5
"Blue Whale".indexOf("Whale",5) // returns 5
"Blue Whale".indexOf("",9)      // returns 9
"Blue Whale".indexOf("",10)     // returns 10
"Blue Whale".indexOf("",11)     // returns 10

The indexOf method is case sensitive. For example, the following expression returns -1:

"Blue Whale".indexOf("blue")

Note that '0' doesn't evaluate to true and '-1' doesn't evaluate to false. Therefore, when checking if a specific string exists within another string the correct way to check would be:

"Blue Whale".indexOf("Blue") != -1 // true
"Blue Whale".indexOf("Bloe") != -1 // false 

Examples

Example: Using indexOf and lastIndexOf

The following example uses indexOf and lastIndexOf to locate values in the string "Brave new world".

var anyString="Brave new world"


document.write("<P>The index of the first w from the beginning is " +
   anyString.indexOf("w"))          // Displays 8

document.write("<P>The index of the first w from the end is " +
   anyString.lastIndexOf("w"))      // Displays 10

document.write("<P>The index of 'new' from the beginning is " +
   anyString.indexOf("new"))        // Displays 6

document.write("<P>The index of 'new' from the end is " +
   anyString.lastIndexOf("new"))    // Displays 6

Example: indexOf and case-sensitivity

The following example defines two string variables. The variables contain the same string except that the second string contains uppercase letters. The first writeln method displays 19. But because the indexOf method is case sensitive, the string "cheddar" is not found in myCapString, so the second writeln method displays -1.

myString="brie, pepper jack, cheddar"
myCapString="Brie, Pepper Jack, Cheddar"
document.writeln('myString.indexOf("cheddar") is ' +
   myString.indexOf("cheddar"))
document.writeln('<P>myCapString.indexOf("cheddar") is ' +
   myCapString.indexOf("cheddar")) 

Example: Using indexOf to count occurrences of a letter in a string

The following example sets count to the number of occurrences of the letter x in the string str:

count = 0;
pos = str.indexOf("x");
while ( pos != -1 ) {
   count++;
   pos = str.indexOf("x",pos+1);
}

See Also

charAt, lastIndexOf, split, Array.indexOf

 

{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String/indexOf", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/indexOf", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/indexOf" } ) }}

Revision Source

<p> </p>
<h3 name="Summary">Summary</h3>
<p>Returns the index within the calling <code>String</code> object of the first occurrence of the specified value, starting the search at <code>fromIndex</code>, <br>
returns -1 if the value is not found.</p>
<table class="standard-table"> <thead> <tr> <th class="header" colspan="2">Method of <a href="/en/JavaScript/Reference/Global_Objects/String"><code>String</code></a></th> </tr> </thead> <tbody> <tr> <td>Implemented in</td> <td>JavaScript 1.0</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMAScript 1st Edition</td> </tr> </tbody>
</table> <h3 name="Syntax">Syntax</h3>
<p><code><em>string</em>.indexOf(<em>searchValue</em>[, <em>fromIndex</em>]</code>)</p><h3 name="Parameters">Parameters</h3>
<dl> <dt><code>searchValue</code> </dt> <dd>A string representing the value to search for.</dd>
</dl>
<dl> <dt><code>fromIndex</code> </dt> <dd>The location within the calling string to start the search from. It can be any integer between 0 and the length of the string. The default value is 0.</dd>
</dl>
<h3 name="Description">Description</h3>
<p>Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character of a string called <code>stringName</code> is <code>stringName.length - 1</code>.</p>
<pre>"Blue Whale".indexOf("Blue")    // returns 0
"Blue Whale".indexOf("Blute")   // returns -1
"Blue Whale".indexOf("Whale",0) // returns 5
"Blue Whale".indexOf("Whale",5) // returns 5
"Blue Whale".indexOf("",9)      // returns 9
"Blue Whale".indexOf("",10)     // returns 10
"Blue Whale".indexOf("",11)     // returns 10
</pre>
<p>The <code>indexOf</code> method is case sensitive. For example, the following expression returns -1:</p>
<pre>"Blue Whale".indexOf("blue")
</pre>
<p>Note that '0' doesn't evaluate to <code>true</code> and '-1' doesn't evaluate to <code>false</code>. Therefore, when checking if a specific string exists within another string the correct way to check would be:</p>
<pre>"Blue Whale".indexOf("Blue") != -1 // true
"Blue Whale".indexOf("Bloe") != -1 // false 
</pre>
<h3 name="Examples">Examples</h3>
<h4 name="Example:_Using_indexOf_and_lastIndexOf">Example: Using <code>indexOf</code> and <code>lastIndexOf</code></h4>
<p>The following example uses <code>indexOf</code> and <code>lastIndexOf</code> to locate values in the string "<code>Brave new world</code>".</p>
<pre>var anyString="Brave new world"


document.write("&lt;P&gt;The index of the first w from the beginning is " +
   anyString.indexOf("w"))          // Displays 8

document.write("&lt;P&gt;The index of the first w from the end is " +
   anyString.lastIndexOf("w"))      // Displays 10

document.write("&lt;P&gt;The index of 'new' from the beginning is " +
   anyString.indexOf("new"))        // Displays 6

document.write("&lt;P&gt;The index of 'new' from the end is " +
   anyString.lastIndexOf("new"))    // Displays 6
</pre>
<h4 name="Example:_indexOf_and_case-sensitivity">Example: <code>indexOf</code> and case-sensitivity</h4>
<p>The following example defines two string variables. The variables contain the same string except that the second string contains uppercase letters. The first <code>writeln</code> method displays 19. But because the <code>indexOf</code> method is case sensitive, the string "<code>cheddar</code>" is not found in <code>myCapString</code>, so the second <code>writeln</code> method displays -1.</p>
<pre>myString="brie, pepper jack, cheddar"
myCapString="Brie, Pepper Jack, Cheddar"
document.writeln('myString.indexOf("cheddar") is ' +
   myString.indexOf("cheddar"))
document.writeln('&lt;P&gt;myCapString.indexOf("cheddar") is ' +
   myCapString.indexOf("cheddar")) 
</pre>
<h4 name="Example:_Using_indexOf_to_count_occurrences_of_a_letter_in_a_string">Example: Using <code>indexOf</code> to count occurrences of a letter in a string</h4>
<p>The following example sets <code>count</code> to the number of occurrences of the letter <code>x</code> in the string <code>str</code>:</p>
<pre>count = 0;
pos = str.indexOf("x");
while ( pos != -1 ) {
   count++;
   pos = str.indexOf("x",pos+1);
}
</pre>
<h3 name="See_Also">See Also</h3>
<p><a href="/en/JavaScript/Reference/Global_Objects/String/charAt" title="en/Core_JavaScript_1.5_Reference/Global_Objects/String/charAt">charAt</a>, <a href="/en/JavaScript/Reference/Global_Objects/String/lastIndexOf" title="en/Core_JavaScript_1.5_Reference/Global_Objects/String/lastIndexOf">lastIndexOf</a>, <a href="/en/JavaScript/Reference/Global_Objects/String/split" title="en/Core_JavaScript_1.5_Reference/Global_Objects/String/split">split</a>, <a class="internal" href="/en/JavaScript/Reference/Global_Objects/Array/indexOf" title="en/Core JavaScript 1.5 Reference/Global Objects/Array/indexOf">Array.indexOf</a></p>
<p> </p>
<p>{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Objetos_globales/String/indexOf", "ja": "ja/Core_JavaScript_1.5_Reference/Global_Objects/String/indexOf", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Obiekty/String/indexOf" } ) }}</p>
Revert to this revision