lastIndexOf (JavaScript)

Gets the index of the last occurrence of a substring.

Defined in

String (Standard - JavaScript)

Syntax

lastIndexOf(searchString:string, position:int) : int

lastIndexOf(searchString:string) : int

Parameters Description
searchString Substring to be found.
position Position from the last position at which the search starts. Defaults to the last position.
Return value Description
int Position of the substring relative to position 0, or -1 if the substring is not found.

Usage

This method searches backwards starting from the last position in the string, or the last position minus the second parameter.

Examples

(1) This example returns Paris.
var cities = new String("Paris   Moscow   Tokyo   Paris");
var n = cities.lastIndexOf("Paris");
if (n >= 0) {
	return cities.substring(n)
} else {
	return "Paris not found";
}

(2) This example returns Paris Moscow Tokyo Paris.

var cities = new String("Paris   Moscow   Tokyo   Paris");
var n = cities.lastIndexOf("Paris", 5);
if (n >= 0) {
	return cities.substring(n)
} else {
	return "Paris not found";
}