Java.lang.String.lastIndexOf() Method

There are four variants of lastIndexOf() method.This article depicts about all of them, as follows:
1. lastIndexOf() : This method returns the index of the last occurrence of the character in the character sequence .

Syntax:
int lastIndexOf(int ch)
Parameters:
ch : a character.
Return Value:
This method returns the index.
filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Found Last Index of g at : 19

2. lastIndexOf(int ch, int beg) : This method returns the index of the last occurrence of the character in the character sequence represented by this object that is less than or equal to beg, or -1 if the character does not occur before that point.



Syntax:
public int lastIndexOf(int ch, int beg)
Parameters:
ch : a character.
beg : the index to start the search from.
Returned Value:
This method returns the index.
filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Found Last Index of g at : 11

3. lastIndexOf(String str) : This method accepts a String as an argument, if the string argument occurs one or more times as a substring within this object, then it returns the index of the first character of the last such substring is returned. If it does not occur as a substring, -1 is returned.

Syntax:
public int lastIndexOf(String str)
Parameters:
str : a string.
Returned Value:
This method returns the index.
filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Found substring geeks at : 19

4. lastIndexOf(String str, int beg) : This method returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.

Syntax:
public int lastIndexOf(String str, int beg)
Parameters
beg : the index to start the search from.
str : a string.
Return Value
This method returns the index.
filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Found substring geeks at : 11

This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : nidhi_biet