Home >Web Front-end >JS Tutorial >Introductory example of JavaScript indexOf method (calculates the position where the specified character first appears in the string)_Basic knowledge

Introductory example of JavaScript indexOf method (calculates the position where the specified character first appears in the string)_Basic knowledge

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 16:33:411683browse

JavaScript indexOf method

The indexOf method is used to calculate the first occurrence position of a specified string in the string and return the value. Its syntax is as follows:

Copy code The code is as follows:

str_object.indexOf( search, start )

Parameter description:

参数 说明
str_object 要操作的字符串(对象)
search 必需。要检索的字符串
start 可选。指定开始检索的位置,如省略该参数,则将从字符串的首字符开始检索

Tip: String counting starts from 0.

indexOf method instance

Copy code The code is as follows:


Run this example, output:

Copy code The code is as follows:

3

indexOf is case-sensitive, if the string value to be retrieved does not appear, the method returns -1.

Statement:
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