Name

String.lastIndexOf( ) Method — find the last occurrence of a substring in a string

Availability

Flash 5

Synopsis

string.lastIndexOf(substring)
string.lastIndexOf(substring, startIndex)

Arguments

substring

A string containing the character or characters to search for.

startIndex

An optional integer position in string at which to start the search for substring. The string is searched backward from startIndex, which should be in the range 0 (the first character) to string .length-1 (the last character). Defaults to string .length-1.

Returns

The position of the last occurrence of substring in string prior to startIndex. Returns -1 if substring is not found prior to startIndex in string.

Description

The lastIndexOf( ) method is used to search for the last occurrence of a substring in a string or to check whether a string contains a certain substring.

Example

URL = "http://www.moock.org/webdesign/flash/fillthewindow.html";
// Finds the last slash character
lastSlash = URL.lastIndexOf("/");
// Extracts the filename from the URL
file = URL.substring(lastSlash + 1);
trace(file);  // Displays: fillthewindow.html

See Also

String.charAt( ), String.indexOf( ); Section 4.6.5.4 in Chapter 4

Get ActionScript: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.