string.lastIndexOf()

Get the index of the last matching substring within a String.

Signature

lastIndexOf(pattern: String) => Number | Null

lastIndexOf(pattern: String, end: Number) => Number | Null

Description

Returns the zero-based offset index for the last occurrence of a provided substring within the calling String. Ends at an optional end position in the calling String.

Parameters

Parameter Type Required Description

pattern

String

true

Substring to find the last occurrence of within the calling String.

end

Int

true

Zero-based index of the character to end searching for matches, counting from left to right. Defaults to the last character of the calling String.

Return value

One of:

Type Description

Int

Zero-based index of the last matching occurrence in the calling String, ending at the specified end character.

Null

No match not found.

Examples

  1. Get the location of the last occurrence of the String "200" at or below index location 27:

    "HTTP/1.1 200 OK - SUCCESS (200)".lastIndexOf("200", 27)
    27
  2. Get the location of the last occurrence of the String "200" at or below index location 20:

    "HTTP/1.1 200 OK - SUCCESS (200)".lastIndexOf("200", 20)
    9
  3. Get the location of the last occurrence of the String "200" at or below index location 8, which fails because the String isn’t found:

    "HTTP/1.1 200 OK - SUCCESS (200)".lastIndexOf("200", 8)
    null
\