JavaScript Glossary: String lastIndexOf()

Publikováno: 27.3.2019

Basics

The lastIndexOf() method returns the last position within a String object which an element can be found. The search starts from the last position of the String object. ...

Celý článek

Basics

The lastIndexOf() method returns the last position within a String object which an element can be found. The search starts from the last position of the String object. The method will return -1 if the specified parameter cannot be found.

"hello world scotch world".lastIndexOf("world")
// 19

https://scotch.io/embed/gist/0d1de6464f68984f5d06f6a0a5b1f833

Syntax

const indexOfLastOccurence = string.lastIndexOf(searchParam, startIndex)

2 Parameters

searchParam This is the string value to search for within the String object - required

startIndex This is the index at which the search will begin. Search typically begins from the string.length - 1 index, which is the last position of the object. If the length of the provided string is 0, or if no item was found, it returns -1.

Returns a number

The method returns a number whch is the index of the last occurence of the search parameter.

This method is case sensitive

Common Uses and Snippets

Find the last word repitition in a list

With the lastIndexOf() method, the index of the last occurrence of a particular string can be obtained.

https://scotch.io/embed/gist/710066198c067d17045b34b57bc486ea

Nahoru
Tento web používá k poskytování služeb a analýze návštěvnosti soubory cookie. Používáním tohoto webu s tímto souhlasíte. Další informace