Home  >  Article  >  Web Front-end  >  Introductory example of JavaScript lastIndexOf method (calculates the position of the last occurrence of the specified character in the string)_Basic knowledge

Introductory example of JavaScript lastIndexOf method (calculates the position of the last occurrence of the specified character in the string)_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:33:391600browse

JavaScript lastIndexOf method

The lastIndexOf method is used to calculate the position of the last occurrence of the specified string in the entire string and return the value. Its syntax is as follows:

Copy code The code is as follows:

str_object.lastIndexOf( search, start )

Parameter description:

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

Tip: String counting starts from 0.

lastIndexOf method instance

Copy code The code is as follows:


Run this example, output:

Copy code The code is as follows:

9
3
9

Note that in the above example where the second parameter is 5, what is actually found is the first . symbol, that is, the search is in the www.5 string.
lastIndexOf is case-sensitive and returns -1 if the string value to be retrieved does not occur.
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