Home >>Java String Methods >Java String lastIndexOf() Method

Java String lastIndexOf() Method

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.

Syntax

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)

Parameters

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

Technical Details

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

Java String lastIndexOf() Method Example 1


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"));
  }
}

Output:
41

Java String lastIndexOf() Method Example 2


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);       
    }  
}  

Output:
11
-1