lastIndexOf()

Get the last occurrence of a substring in the string.

Signature

lastIndexOf(otherString: String, position: Int): Int

Description

The lastIndexOf() method returns the starting location of last occurrence of otherString in the index range indicated by position. If position is positive, the method terminates the search at the zero-based position index, counting from the left.

Parameters

Parameter Type Required Description

otherString

String

Yes

String to search for in this string.

position

Int

Yes

If position is positive, the index location to terminate the search at, counting from left to right.

Return value

One of:

Type Description

Int

The index location of the last occurring string that matches otherString, in the range described by position.

Null

Exactly matching string 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

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!