How do you find the index of a particular character in a string in javascript?

Examples

Search a string for "welcome":

let text = "Hello world, welcome to the universe.";
let result = text.indexOf["welcome"];

Try it Yourself »

Search a string for "Welcome":

let text = "Hello world, welcome to the universe.";
let result = text.indexOf["Welcome"];

Try it Yourself »

Find the first occurrence of "e":

let text = "Hello world, welcome to the universe.";
text.indexOf["e"];

Try it Yourself »

Find the first occurrence of "e", starting at position 5:

let text = "Hello world, welcome to the universe.";
text.indexOf["e", 5];

Try it Yourself »

Find the first occurrence of "a":

let text = "Hello world, welcome to the universe.";
text.indexOf["a"];

Try it Yourself »

Definition and Usage

The indexOf[] method returns the position of the first occurrence of a value in a string.

The indexOf[] method returns -1 if the value is not found.

The indexOf[] method is case sensitive.

Syntax

string.indexOf[searchvalue, start]

Parameters

Parameter Description
searchvalue Required.
The string to search for.
start Optional.
The position to start from [default is 0].

Return Value

Type Description
A number The first position where the search-value occurs.
-1 if it never occurs.

The Differense Between
String indexOf[] and String search[]

The indexOf[] method cannot search against a regular expression.

The search[] cannot take a start position argument.

Browser Support

indexOf[] is an ECMAScript1 [ES1] feature.

ES1 [JavaScript 1997] is fully supported in all browsers:

Chrome IE Edge Firefox Safari Opera
Yes Yes Yes Yes Yes Yes

The indexOf[] method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the first occurrence of the specified substring. Given a second argument: a number, the method returns the first occurrence of the specified substring at an index greater than or equal to the specified number.

Try it

Syntax

indexOf[searchString]
indexOf[searchString, position]

Parameters

searchString

Substring to search for, coerced to a string.

If the method is called with no arguments, searchString is coerced to "undefined". Therefore,"undefined".indexOf[] returns 0 — because the substring "undefined" is found at position 0 in the string "undefined". But "undefine".indexOf[], returns -1 — because the substring "undefined" is not found in the string "undefine".

position Optional

The method returns the index of the first occurrence of the specified substring at a position greater than or equal to position, which defaults to 0. If position is greater than the length of the calling string, the method doesn't search the calling string at all. If position is less than zero, the method behaves as it would if position were 0.

  • 'hello world hello'.indexOf['o', -5] returns 4 — because it causes the method to behave as if the second argument were 0, and the first occurrence of o at a position greater or equal to 0 is at position 4.
  • 'hello world hello'.indexOf['world', 12] returns -1 — because, while it's true the substring world occurs at index 6, that position is not greater than or equal to 12.
  • 'hello world hello'.indexOf['o', 99] returns -1— because 99 is greater than the length of hello world hello, which causes the method to not search the string at all.

Return value

The index of the first occurrence of searchString found, or -1 if not found.

Return value when using an empty search string

Searching for an empty search string produces strange results. With no second argument, or with a second argument whose value is less than the calling string's length, the return value is the same as the value of the second argument:

'hello world'.indexOf[''] // returns 0
'hello world'.indexOf['', 0] // returns 0
'hello world'.indexOf['', 3] // returns 3
'hello world'.indexOf['', 8] // returns 8

However, with a second argument whose value is greater than or equal to the string's length, the return value is the string's length:

'hello world'.indexOf['', 11] // returns 11
'hello world'.indexOf['', 13] // returns 11
'hello world'.indexOf['', 22] // returns 11

In the former instance, the method behaves as if it found an empty string just after the position specified in the second argument. In the latter instance, the method behaves as if it found an empty string at the end of the calling string.

Description

Strings are zero-indexed: The index of a string's first character is 0, and the index of a string's last character is the length of the string minus 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['Whale', 7]  // returns -1
'Blue Whale'.indexOf['']          // returns  0
'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']  // returns -1

Checking occurrences

When checking if a specific substring occurs within a string, the correct way to check is test whether the return value is -1:

'Blue Whale'.indexOf['Blue'] !== -1  // true; found 'Blue' in 'Blue Whale'
'Blue Whale'.indexOf['Bloe'] !== -1  // false; no 'Bloe' in 'Blue Whale'

Examples

Using indexOf[]

The following example uses indexOf[] to locate substrings in the string "Brave new world".

const str = 'Brave new world';

console.log[`Index of first w from start is ${str.indexOf['w']}`]; // logs 8
console.log[`Index of "new" from start is ${str.indexOf['new']}`]; // logs 6

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 console.log[] method displays 19. But because the indexOf[] method is case sensitive, the string "cheddar" is not found in myCapString, so the second console.log[] method displays -1.

const myString = 'brie, pepper jack, cheddar';
const myCapString = 'Brie, Pepper Jack, Cheddar';

console.log[`myString.indexOf["cheddar"] is ${myString.indexOf['cheddar']}`];
// logs 19
console.log[`myCapString.indexOf["cheddar"] is ${myCapString.indexOf['cheddar']}`];
// logs -1

Using indexOf[] to count occurrences of a letter in a string

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

const str = 'To be, or not to be, that is the question.';
let count = 0;
let position = str.indexOf['e'];

while [position !== -1] {
  count++;
  position = str.indexOf['e', position + 1];
}

console.log[count]; // displays 4

Specifications

Specification
ECMAScript Language Specification
# sec-string.prototype.indexof

Browser compatibility

BCD tables only load in the browser

See also

How do you find the index of a certain character in a string?

The indexOf[] method returns the position of the first occurrence of specified character[s] in a string. Tip: Use the lastIndexOf method to return the position of the last occurrence of specified character[s] in a string.

How do I see all indexes in a string?

To get all indexes of a specific character in a string: Create an empty array that will store the indexes. Use a for loop and loop from 0 up to the string's length. For each iteration of the loop, check if the character at that index is equal to the specific character.

How do you find the last index of a character in a string in JavaScript?

The lastIndexOf[] method returns the index [position] of the last occurrence of a specified value in a string. The lastIndexOf[] method searches the string from the end to the beginning. The lastIndexOf[] method returns the index from the beginning [position 0].

What does indexOf do in JavaScript?

indexOf[] The indexOf[] method, given one argument: a substring to search for, searches the entire calling string, and returns the index of the first occurrence of the specified substring.

What is the index of the first character in a string?

The first character in a string is present at index zero and the last character in a string is present at index length of string-1 .

Can index be used on strings?

Strings are ordered sequences of character data, 00:15 and the individual characters of a string can be accessed directly using that numerical index. String indexing in Python is zero-based, so the very first character in the string would have an index of 0 , 00:30 and the next would be 1 , and so on.

Chủ Đề