Open In App

String lastIndexOf() Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The String lastIndexOf() method returns the position of the last occurrence of a given character or substring in a String.

The Index starts from 0, and if the given substring is not found it returns -1.

Example:

Java




// Java program to demonstrate
// the lastIndexOf(char ch) method
 
public class L_index1 {
    public static void main(String args[]) {
        // Creating a string
        String Str = new String("Hello World");
         
        // Printing message before finding last index of 'l'
        System.out.print("Found Last Index of l at : ");
         
        // Finding and printing the last index of 'l'
        System.out.println(Str.lastIndexOf('l'));
    }
}


Output

Found Last Index of l at : 9


Syntax

There are four variants of the lastIndexOf() method.

Method Description
int lastIndexOf(char ch) It returns the index of the last occurrence of the specified character.
public int lastIndexOf(char ch, int fromIndex) It returns the index of the last occurrence of the specified character, searching backward starting at the specified index.
public int lastIndexOf(String str)  It returns the index of the last occurrence of the specified substring.
public int lastIndexOf(String str, int fromIndex) It returns the index of the last occurrence of the specified substring, searching backward starting at the specified index. 

Parameters

  • ch- Character value to be searched
  • fromIndex-  index position from where the index of the char value or substring is returned.
  • str- substring to be searched

Returns

  • last index of char or substring.

Java String lastIndexOf() Examples

The following examples demonstrate how to use lastIndexOf() method in Java:

Example:

To show the working of the lastIndexOf() method.

Java




// Java program to demonstrate
// the lastIndexOf(char ch) method
 
public class LastIndexOfExample {
    public static void main(String[] args) {
        // Define a string and a character to search for
        String str = "GeeksforGeeks";
        char ch = 'G';
 
        // Search for the last occurrence of the character in the string
        int lastIndex = str.lastIndexOf(ch);
 
        // Print the last index of the character
        System.out.println("Last index of '" + ch + "': " + lastIndex);
    }
}


Output

Last index of 'G': 8



Java String lastIndexOf(char ch) Method Example: 

To show the working of the lastIndexOf(char ch) method.

Java




// Java code to demonstrate the
// working of lastIndexOf()
public class L_index1 {
 
public static void main(String args[])
    {
 
        // Initialising String
        String Str = new String("Welcome to geeksforgeeks");
 
        System.out.print("Found Last Index of g at : ");
 
        // Last index of 'g' will print
        // prints 19
        System.out.println(Str.lastIndexOf('g'));
    }
}


Output:

Found Last Index of g at : 19

Java String lastIndexOf(char ch, int fromIndex) Method Example:

To show the working of the lastIndexOf(char ch, int fromIndex) method.

Java




// Java code to demonstrate the
// working of lastIndexOf()
public class L_index2 {
 
public static void main(String args[])
    {
 
        // Initialising String
        String Str = new String("Welcome to geeksforgeeks");
 
        System.out.print("Found Last Index of g at : ");
 
        // Last index of 'g' before 15 will print
        // prints 11
        System.out.println(Str.lastIndexOf('g', 15));
    }
}


Output:

Found Last Index of g at : 11

Java lastIndexOf(String str) Example:

To show the working of the lastIndexOf(String str) method.

Java




// Java code to demonstrate the
// working of lastIndexOf(String str)
public class L_index3 {
 
public static void main(String args[])
    {
 
        // Initialising String
        String Str = new String("Welcome to geeksforgeeks");
 
        System.out.print("Found substring geeks at : ");
 
        // start index of last occurrence of "geeks'
        // prints 19
        System.out.println(Str.lastIndexOf("geeks"));
    }
}


Output:

Found substring geeks at : 19

Java String lastIndexOf(String str, int fromIndex) Method Example

To show the working of the lastIndexOf(String str, int fromIndex) method.

Java




// Java code to demonstrate the
// working of lastIndexOf(String str,  int beg)
public class L_index4 {
 
public static void main(String args[])
    {
 
        // Initialising String
        String Str = new String("Welcome to geeksforgeeks");
 
        System.out.print("Found substring geeks at : ");
 
        // start index of last occurrence of "geeks'
        // before 15
        // prints 11
        System.out.println(Str.lastIndexOf("geeks", 15));
    }
}


Output:

Found substring geeks at : 11

References

To know more about more String Methods refer to the article Java String Methods

Whether you are a beginner starting Java programming or an experienced looking to brush up on your Java skills, this tutorial will provide you with a deep understanding of the lastIndexOf function and its uses in Java.

The charAt method in Java is a fundamental function for string manipulation. With this guide, you can easily get the last index of a substring using the lastIndexOf function.



Last Updated : 16 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads