lastIndexOf() in Java
Overview
The lastIndexOf() is a method in Java that returns the last index of a substring or a character from the given string. It is similar to indexOf() method. The main difference is that the lastIndexOf() returns the position of the last occurrence of the specified character or string, whereas the indexOf() method returns the position of the first occurrence.
Syntax of lastIndexOf() in Java
There are four syntaxes of lastIndexOf() as given below:
Parameters of lastIndexOf() in Java
- String st: It represents the string to search for.
- int ch: An int value, representing a single character to search for.
- int parameter2:
- It is an optional argument.
- If passed, the lastIndexOf method finds the character or the string from the first parameter2 characters.
- Default value is length of the string (which is searched).
Return Value of lastIndexOf() in Java
Return Type: int.
- It returns an integer value that indicates the index of the last occurrence of the character or the string which is searched.
- Returns -1 if not found.
Example
Let's see an example of the lastIndexOf().
Code
Output
Explanation
- In the above example, first, we have declared the array of strings named arr. Now we are supposed to find the index of the last occurrence of the letter @ in each string of the array.
- After finding the index of the @ from the strings representing the email ids, we now print the name using substring() method.
- The substring() method needs two arguments, i.e., starting and ending points of the substring.
- Here, the starting index will be 0 and ending index will be the index of the last occurrence of the letter @.
What is lastIndexOf() in Java?
The lastIndexOf() method in java returns the last occurrence of a specific character or a specific substring in the string; if the occurrence is not found in the original string, it returns -1.
To find the last occurrence, it starts its search from the end of the string and continues backward, i.e., from right to left. If the value of optional argument is supplied, i.e., parameter2 then the search starts from parameter2 and continues till the zeroth index of the string.
The following image shows how the function will work when with and without parameter2.
For example, the below image shows the working of the lastIndexOf() function.
Variants of the lastIndexOf() Method in Java
The four variants of lastIndexOf() are as follows:
lastIndexOf(char ch)
This method takes an argument as a character, and if the character occurs in the string, it returns the index of the last occurrence of the character. If the character doesn't appear in the string the function returns -1.
Let us see an example to understand the above explanation.
Code
Output
Explanation
- In the above example, we are declaring two variables, lastIndexChar1 and lastIndexChar2.
- The lastIndexChar1 stores the index of the last occurrence of the character ‘a’ from the original string s. The last occurrence of the character a is found on index 13.
- The lastIndexChar2 stores the index of the last occurrence of the character ‘z’ from the original string s.
- It outputs the value -1 as there is no character ‘z’ in the original string s.
2. lastIndexOf(char ch, int num)
This method consists of two arguments, the first is the character, and the second is an integer. Here, the search for a character starts from the index num. The search goes from the index num to the index 0 implicitly. When the character ch is encountered, the function returns the current position. If the function fails to find the character ch, it returns -1.
Let us see the implementation now.
Code
Output
Explanation
- In the above code, we have declared two variables, i.e., lastIndexChar1 and lastIndexChar2. The lastIndexChar1 stores the last occurrence of the character ‘e’ in the substring of the first 17 characters of string s.
- On the 15th index, we get the last occurrence of a character ‘e’.
- The lastIndexChar2 stores the last occurrence of the character ‘e’ in the substring of the first 10 characters of string s.
- The value of lastIndexChar2 is 6 as the substring of the first 10 characters contains the letter ‘e’ at 6th index.
3. lastIndexOf(string sub)
This method takes an argument sub as a string, and if the substring occurs in the string, it returns the index of the last occurrence of the substring from the string s which will be searched. If the substring sub is not found in the string s, it returns -1.
Let us see the practical implementation of the above method.
Code
Output
Explanation
- In the above example, we are declaring two variables, lastIndexSubstr1 and lastIndexSubstr2. The lastIndexSubstr1 stores the index of the last occurrence of the substring “Scaler” from the original string s.
- The last occurrence of the word “Scaler” is found on index 11.
- The lastIndexSubstr2 stores the index of the last occurrence of the substring “Welcome” from the original string s.
- It outputs the value 0 as the substring “Welcome” is right at the start of string s.
4. lastIndexOf(string sub, index num)
This method consists of two arguments, the first is a string, and the second is an integer. Here, the search for a string sub starts from the index num. The search goes from num to 0. When the substring sub is encountered, the function returns the current position. If the function fails to find the substring, it returns -1.
Let us implement the code for the above explanation.
Code
Output
Explanation
- In the above code, we have declared two variables, i.e., lastIndexSubstr1 and lastIndexSubstr2. The lastIndexSubstr1 stores the value 11 as it is the index of the last occurence of the word "Scaler" in the substring of first 17 characters.
- The lastIndexSubstr2 stores the last occurrence of the word “Scaler” in the substring of the first 10 characters of string s. But the value of lastIndexSubstr2 is -1 as the substring of the first 10 characters does not contain the word “Scaler”.
More Examples
Let us see some examples where lastIndexOf() can be used.
Example 1:
Consider any string s and find the second last occurrence of a letter say ‘a’ from the given string s.
Approach: Other than the naive approach we can run nested lastIndexOf() function to get the second last occurrence of the letter a from the string.
Code
Output
Explanation
- In the above example, we are using nested lastIndexOf() calls to get the second last occurence of character 'e' in the string s.
- When we run the lastIndexOf() for the first time, we get the index of the first occurrence of the letter 'e'. Now, this index will act as a parameter2, and we run the lastIndexOf() function one more time.
- Now, we will get the second last occurrence of the letter 'e'.
- In this case we have used two variations of the lastIndexOf() method, i.e., lastIndexOf(char), lastIndexOf(char, int) at the same place.
Example 2:
Consider a folder holding multiple files. You are supposed to extract all the files that have an extension of .txt. Also, print the name of the file without .txt extension.**
Approach:
- First we will check if the extension of the file is .txt by using the lastIndexOf() function. Suppose the output of lastIndexOf() function is -1 then the extension of the file is not .txt.
- Now after we get the file having extension .txt we will remove the extension using substring() and lastIndexOf() methods as shown in the following code.
Code
Output
Explanation
- In the above example, we are storing the names (along with the extensions) of all the files in the folder array.
- Furthermore, we are running a for loop over the folder array and checking if the file name has an extension of .txt using lastIndexOf() function.
- If the file has an extension of .txt, we are printing the name of the file by removing .txt from the filename. We remove this extension using substring() and lastIndexOf() methods.
Example 3: NullPointerException
A null value cannot be passed as an argument to lastIndexOf() method as it will throw NullPointerException.
Let us understand this by an example:
Code:
Output
Explanation:
As we can see that passing null value as an argument into the lastIndexOf() method call throws an exception.
Conclusion
- The lastIndexOf() is a method in Java that returns the last index of a substring or a character from the given string.
- The indexOf() method in Java returns the index of the first occurrence of a specified substring or character in a string, while the lastIndexOf() method returns the index of the last occurrence of the specified substring or character in a string.
- The time complexity of the lastIndexOf() method in Java is O(n), where n is the length of the string.
- This is because the method iterates over the string from the end to the beginning until it finds the last occurrence of the specified substring or reaches the beginning of the string.
- It reduces the length of the code and makes it more understandable.
- The index returned by the lastIndexOf() function is based on zero-indexing method.
- Nested calls of lastIndexOf() method can be used to find the higher order occurrences of a character or substring in a given string.