Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaJava.lang.String.lastIndexOf() Method

Java.lang.String.lastIndexOf() Method

There are four variants of lastIndexOf() method.This article depicts 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(char ch)
Parameters:
ch : a character.
Return Value:
This method returns the index.

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 neveropen");
 
        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(char 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(char ch, int beg)
Parameters:
ch : a character.
beg : the index to start the search from.
Returned Value:
This method returns the index.

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




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




// 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 neveropen");
 
        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 Lazyroar and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.

RELATED ARTICLES

Most Popular

Recent Comments