Home > Web Front-end > JS Tutorial > How to use indexof method

How to use indexof method

不言
Release: 2019-01-26 17:46:55
Original
45111 people have browsed it

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.

How to use indexof method

Let’s first look at the basic syntax of indexof

string.indexOf(searchValue[, fromIndex])
Copy after login

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>
Copy after login

The display effect on the browser is as follows

How to use indexof method

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template