The lastIndexOf() method gives the index (position) of the string’s last instance of a given value. The string is traversed beginning at the end via the lastIndexOf() method. The starting index (position 0) is returned by the lastIndexOf() method.
Definition of Java String lastIndexOf() Method:
The position of the final instance of the provided character(s) in a string is returned by the lastIndexOf() function. To find the first instance of the supplied character(s) in a string, use the indexOf method.
This method returns the index of the character’s last occurrence, less than or equal to beg, in the character sequence that this object represents, or -1 if the character doesn’t appear before that point.
Syntax:
- string.lastIndexOf(int ch, int index)
- string.lastIndexOf(string str, int index)
There are 4 lastIndexOf() methods:
Table:
public int lastIndexOf(String str) | For the specified substring, it returns the latest index point. |
public int lastIndexOf(String str, int fromIndex) | For the specified substring, it returns the last index position, starting at index. |
public int lastIndexOf(int char) | The specified char value’s last index position is returned. |
public int lastIndexOf(int char, int fromIndex) | It returns the last index position from index for the specified char value. |
Return Value:
If the character doesn’t appear at all, -1; otherwise, an int value denoting the character’s first occurrence in the string.
The index is returned by this procedure.
Parameters:
str A String value that represents the string to be searched for in fromIndexAn int integer that represents the starting point for the search. The length of the string char is the default value if absent. A Unicode value or an int value that represents a single character, such as “A,”.
Example 1:
public class Main { public static void main(String[] args) { String myStr = "Hello planet earth, you are a great planet."; System.out.println(myStr.lastIndexOf("e", 5)); } }
Output:
1
Example 2:
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 : "); System.out.println(Str.lastIndexOf('g')); } }
Output:
Found Last Index of g at 19
Diagram:
Case sensitive:
The indexOf() method returns -1 if the target string cannot be found or the index number where it is first discovered. Because the indexOf() method is case-sensitive, like equals(), uppercase and lowercase characters are treated differently.
Time complexity:
The lastIndexOf() function in Java has an O(n) time complexity, where n is the string’s length. This is the case because the procedure loops through the entire string from beginning to end until it finds the final instance of the requested substring or gets to the beginning of the string.
Exception:
The value of the substring is passed as a parameter to the lastIndexOf() function as a string. If the supplied string is null, an exception is thrown. In the event that the substring cannot be discovered, the procedure returns -1.
Advantages:
- It is a built-in method in Java.
- It is efficient and fast.
- It can be used to search for any substring.
Disadvantages:
- It can only be used to search for substrings.
- If the substring does not occur in the string, the method returns-1.
- If the string is very large, The method can be slow.
Conclusion
This has taught us the syntax for the Java lastIndexOf() function and, with the help of examples, how to utilise it.