Home > Article > Web Front-end > How to use indexof method
indexOf method is used to search the index of array elements. It retrieves whether an array contains a given element. If a given element in the How to use indexof method indexOf method is found, it will return the index number of the element. Let's look at the specific usage of the indexof method.
Let’s first look at the basic syntax of indexof
string.indexOf(searchValue[, fromIndex])
searchValue is a string representing the value to be searched.
fromIndex represents the position in the calling string used to start the search. It can be any integer between 0 and the length of the string. The default value is 0.
Start searching from fromIndex, and return -1 if the value is not found.
Let’s look at a specific example
The code is as follows
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <script type = "text/javascript"> var str1 = new String( "This is string one" ); var index = str1.indexOf( "string" ); document.write("indexOf found String :" + index ); document.write("<br />"); var index = str1.indexOf( "one" ); document.write("indexOf found String :" + index ); </script> </body> </html>
The display effect on the browser is as follows
This article ends here. For more detailed usage of the index() method, you can refer to How to use indexof in How to use indexof methodthis article! ! !
The above is the detailed content of How to use indexof method. For more information, please follow other related articles on the PHP Chinese website!