Java - String lastIndexOf() Method
The Java string lastIndexOf() method is used to find out the index number for last occurrence of specified character in the given string.
Syntax
public int lastIndexOf(int ch)
Parameters
ch |
specify the character to search for. |
Return Value
Returns the index number for last occurrence of specified character. Returns -1 if there is no such occurrence.
Exception
NA.
Example:
In the example below, lastIndexOf() method is used to find out the index number for last occurrence of specified character in the given string.
import java.lang.*; public class MyClass { public static void main(String[] args) { String MyString = "Java is a programming language. Learning Java is fun."; //index number of last occurrence //of 'J' in the given string System.out.println(MyString.lastIndexOf('J')); } }
The output of the above code will be:
41
❮ Java.lang - String