The indexOf() method returns the indexOf the specified value that first occurs in the String calling it, searching from fromIndex. If the value is not found, -1 is returned.

grammar

str.indexOf(searchValue [, fromIndex])
Copy the code

parameter

searchValue

  • The string value to be looked up.

  • If the exact supply string is not provided, the searchValue is forced to be “undefined” and then looks for that value in the current string.

  • For example: ‘undefined’.indexof () will return 0 because undefined was found at position 0, but ‘undefine’.indexof () will return -1 because the string ‘undefined’ was not found.

FromIndex optional

  • The number indicates where the search began. Can be any integer. The default value is 0.

  • If fromIndex is less than 0, or greater than str.length, the search starts at 0 and str.length, respectively. (If fromIndex is less than 0, it is null; If fromIndex is greater than or equal to str.length, -1 is returned.)

  • For example, ‘Hello world’.indexof (‘o’, -5) returns 4 because it starts at position 0 and o is found at position 4. On the other hand, ‘Hello world’.indexof (‘o’, 11) (or fromIndex fills in any value greater than 11) will return -1 because the search started at 11, which is already the end of the string.

The return value

The index of the first occurrence of the searched string searchValue, or -1 if not found.

If the searched string searchValue is an empty string, “strange” results will be produced. If the fromIndex value is empty, or if the fromIndex value is less than the length of the string being searched, the return value is the same as the following fromIndex values:

'hello world'.indexOf(' ') / / returns 0
'hello world'.indexOf(' '.0) / / returns 0
'hello world'.indexOf(' ', -2) / / returns 0
'hello world'.indexOf(' '.3) / / return 3
'hello world'.indexOf(' '.8) / / return 8
Copy the code

Alternatively, if fromIndex is greater than or equal to the length of the string, the length of the string is returned directly (str.length) :

'hello world'.indexOf(' '.11) / / return 11
'hello world'.indexOf(' '.13) / / return 11
'hello world'.indexOf(' '.22) / / return 11
Copy the code

As you can see from the previous example, Javascript returns the specified index value directly when the value being looked for is null. As you can see from the following example, Javascript returns the length of the string directly when the value being looked for is null.

describe

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 the string named stringName is stringname.length-1.

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

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

"Blue Whale".indexOf("blue")      / / return 1
Copy the code

Checks for the presence of a string

Note that 0 is not treated as true and -1 is not treated as false. So when checking whether a string exists in another string, use the following method:

'Blue Whale'.indexOf('Blue')! = = -1    // true
'Blue Whale'.indexOf('Bloe')! = = -1    // false~ ('Blue Whale'.indexOf('Bloe'))        // this is an error
Copy the code

The sample

Use indexOf() and lastIndexOf()

The following example uses the indexOf() and lastIndexOf() methods to locate the value of “Brave New World “in the string.

var anyString = "Brave new world";

console.log("The index of the first w from the beginning is " + anyString.indexOf("w"));
// logs 8
console.log("The index of the first w from the end is " + anyString.lastIndexOf("w"));
// logs 10

console.log("The index of 'new' from the beginning is " + anyString.indexOf("new"));
// logs 6
console.log("The index of 'new' from the end is " + anyString.lastIndexOf("new"));
// logs 6
Copy the code

IndexOf and case sensitive

The following example defines two string variables. Both variables contain the same string, except that some characters in the second string are uppercase. The first log method prints 19. However, since indexOf is case sensitive, the string “cheddar” will not be found in myCapString, so the second log method will print -1.

var myString    = "brie, pepper jack, cheddar";
var 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
Copy the code

Use indexOf to count the number of occurrences of a particular letter in a string

In the following example, count is set to count the number of occurrences of the letter e in the string STR:

To be or not to be? That's a problem. (Shakespeare's Hamlet)
var str = 'To be, or not to be, that is the question.';
var count = 0;
var pos = str.indexOf('e');

while(pos ! = = -1) {
  count++;
  pos = str.indexOf('e', pos + 1);
}

console.log(count); // displays 4
Copy the code