Java String.lastIndexOf()

The method lastIndexOf() returns the index of the last occurrence of a
String in another String. If an int is passed to the method, then
the method searches for the Unicode character equivalent.

We can also pass the index of the character to start searching from.

Available Signatures

public int lastIndexOf(int ch)
public int lastIndexOf(int ch, int fromIndex)
public int lastIndexOf(String str)

Example

@Test
public void whenCallLastIndexOf_thenCorrect() {
    assertEquals(2, "foo".lastIndexOf("o"));
    assertEquals(2, "foo".lastIndexOf(111));
}

Next »

« Previous

Leave a Reply

Your email address will not be published.