Home >>Java String Methods >Java String lastIndexOf() Method
Java lastIndexOf() method in Java String is used to returns the position of the last occurrence of specified character in a string.
There are 4 lastIndexOf() methods:
public int lastIndexOf(String str) public int lastIndexOf(String str, int fromIndex) public int lastIndexOf(int char) public int lastIndexOf(int char, int fromIndex)
str - This parameter is used to represents A String value in the string to search for
fromIndex - This parameter is used to represents An int value of the index position to start the search from.
char - This parameter is used to represents An int value of a single character, e.g 'A', or a Unicode value
Returns - This parameter is used to represents An int value the index of the first occurrence of the character in the string, or -1 if it never occurs
public class LastIndexOfexample1
{
public static void main(String[] args)
{
String myStrln1 = "Education makes a door to bright future, Education is must to all";
System.out.println(myStrln1.lastIndexOf("Education"));
}
}
public class LastIndexOfexample2
{
public static void main(String[] args)
{
String Name = "Welcome to the Phptpoint";
int index = Name.lastIndexOf("the", 25);
System.out.println(index);
index = Name.lastIndexOf("the", 10);
System.out.println(index);
}
}