Java String lastIndexOf() Method with Examples
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
The lastIndexOf() method in Java is an extremely useful method for searching within strings. It allows you to find the last occurrence of a character or substring within a larger string.
The String class in Java has four overloaded variants of the lastIndexOf() method to handle different use cases. These variants give you control over what you are searching for (character or substring) and where to start the search from.
Overview Java lastIndexOf() Method
The lastIndexOf() method returns the index of the last occurrence of the character or substring passed to it. The search begins from the end of the string and goes backwards. This allows you to conveniently find the last match instead of needing to search from the start.
Some key points about the lastIndexOf() method:
- It is overloaded with 4 variants to handle different parameters
- The search is done backwards from the end of the string
- It returns the index where the match is found
- If no match is found, it returns -1
The 4 overloaded variants allow you to:
- Search for a single character
- Search for a character with a starting index
- Search for a substring
- Search for a substring with a starting index
Variant 1: lastIndexOf(char ch)
The first variant allows searching for the last occurrence of a character within the string.
public int lastIndexOf(char ch)
The only parameter it takes is the character to search for. Let’s see an example:
public class LastIndexOfExample { public static void main(String[] args) { String str = "Hello World"; int result = str.lastIndexOf('o'); System.out.println("The last index of 'o' in the string is: " + result); } }
Output:
The last index of ‘o’ in the string is: 7
Here, we are calling lastIndexOf() on str to find the last occurrence of the letter ‘o’. This would return 7, which is the index of ‘o’ in “World”.
Variant 2: lastIndexOf(char ch, int fromIndex)
The second variant allows specifying an index to start the backwards search.
public int lastIndexOf(char ch, int fromIndex)
The parameters are:
- ch – The character to search for
- fromIndex – The index to start the search backwards from
Example:
public class LastIndexOfExample { public static void main(String[] args) { String str = "Hello World"; int result = str.lastIndexOf('o', 6); System.out.println("The last index of 'o' before index 6 is: " + result); } }
Output:
The last index of ‘o’ before index 6 is: 4
Here, we are searching backwards from index 6 for the last ‘o’. This would return 4, which is the index of the ‘o’ in “Hello”.
Variant 3: lastIndexOf(String substring)
This variant allows you to search for the last occurrence of a substring within the string.
public int lastIndexOf(String substring)
Example:
public class LastIndexOfExample { public static void main(String[] args) { String str = "Hello World"; int result = str.lastIndexOf("World"); System.out.println("The last index of 'World' in the string is: " + result); } }
Output:
The last index of ‘World’ in the string is: 6
This would return 6, which is the starting index of the substring “World”.
Variant 4: lastIndexOf(String substring, int fromIndex)
The last variant combines substring search with specifying the starting index for backwards search.
public int lastIndexOf(String substring, int fromIndex)
The parameters are:
- substring – The substring to search for
- fromIndex – The index to start search backwards from
public class LastIndexOfExample { public static void main(String[] args) { String str = "Hello World. Hello All."; int result = str.lastIndexOf("Hello", 15); System.out.println("The last index of 'Hello' before index 15 is: " + result); } }
Output:
The last index of ‘Hello’ before index 15 is: 13
Conclusion
In conclusion, the lastIndexOf() method in Java proves to be an invaluable asset for searching within strings. With its four overloaded variants, it empowers you to efficiently locate the final instance of a character or substring, all while granting the flexibility to initiate the search from a specified starting point. This adaptability and control enhance its utility in various string manipulation tasks, simplifying the process of finding and extracting information from strings, whether it’s character-based or substring-based searches, ultimately improving your string-handling capabilities in Java applications.
We work very hard to provide you quality material
Could you take 15 seconds and share your happy experience on Google