Java.lang.String.lastIndexOf() Method

30 Sep, 2019
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.

// 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

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.

// 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

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.

// 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

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.

// 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

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.
nidhi_biet
Java-Functions
Java-lang package
Java-Strings
Java
Java-Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Stream In Java
Collections in Java
Multidimensional Arrays in Java
Singleton Class in Java
Set in Java