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 »
Java String.regionMatches()
« Previous
Java String.isEmpty()
Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.