SoFunction
Updated on 2025-03-01

Detailed analysis of the usage of indexof in js

Method (Char, [startIndex], [count])

Reports the index of the first match of the specified character in this instance. Search starts with the specified character position and checks for the specified number of character positions.

parameter
 
value

Unicode characters to look for. Searches for value are case sensitive.

startIndex(Int32)
 
Optional, search for the starting position. If not set, starts from 0.

count(Int32)
 
Optional, the number of characters to check.

Return value
 
If the character is found, the index position of value; otherwise, -1 if not found.

IndexOf()
 
Find the first occurrence of the specified character or string in the string, and return the first index value, such as:

("word"); //Find the index value (position) of "word" in str1 (position)

("String"); //Find the index value (position) of the first character of "String" in str1 (position)

("word",start,end); //From the start+1 character of str1, find the end characters, and find the position of "word" in the string STR1 [calculated from the first character] Note: start+end cannot be greater than the length of str1

The indexof parameter is string. Find the first occurrence of the parameter string in the string and return to that position. For example, string s="0123dfdfdf";int i=("df"); at this time i==4.

If you need more powerful string parsing functions, you should use the Regex class and use regular expressions to match the string.