String.lastIndexOf() method in Java is used to find the index of the last occurrence of a specified character or substring within a string. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
lastIndexOfMethod Syntax- Examples
- Finding the Last Index of a Character
- Finding the Last Index of a Substring
- Starting Search from a Specific Index
- Handling Not Found Cases
- Conclusion
Introduction
The String.lastIndexOf() method is a member of the String class in Java. It allows you to locate the position of the last occurrence of a character or substring within a string. This method is particularly useful for searching, parsing, and manipulating strings based on specific characters or patterns.
lastIndexOf Method Syntax
The lastIndexOf method has several overloads:
- Finding the last index of a character:
public int lastIndexOf(int ch)
- Finding the last index of a character starting from a specified index:
public int lastIndexOf(int ch, int fromIndex)
- Finding the last index of a substring:
public int lastIndexOf(String str)
- Finding the last index of a substring starting from a specified index:
public int lastIndexOf(String str, int fromIndex)
Examples
Finding the Last Index of a Character
The lastIndexOf method can be used to find the last occurrence of a specified character within a string.
Example
public class LastIndexOfExample {
public static void main(String[] args) {
String message = "Hello, World!";
int lastIndexOfO = message.lastIndexOf('o');
int lastIndexOfL = message.lastIndexOf('l');
System.out.println("Last index of 'o': " + lastIndexOfO);
System.out.println("Last index of 'l': " + lastIndexOfL);
}
}
Output:
Last index of 'o': 8
Last index of 'l': 10
Finding the Last Index of a Substring
The lastIndexOf method can be used to find the last occurrence of a specified substring within a string.
Example
public class LastIndexOfExample {
public static void main(String[] args) {
String message = "Hello, World! Hello, Java!";
int lastIndexOfHello = message.lastIndexOf("Hello");
int lastIndexOfJava = message.lastIndexOf("Java");
System.out.println("Last index of 'Hello': " + lastIndexOfHello);
System.out.println("Last index of 'Java': " + lastIndexOfJava);
}
}
Output:
Last index of 'Hello': 14
Last index of 'Java': 21
Starting Search from a Specific Index
The lastIndexOf method can be used to start the search from a specified index.
Example
public class LastIndexOfExample {
public static void main(String[] args) {
String message = "Hello, World! Hello, Java!";
int lastIndexOfOFromIndex = message.lastIndexOf('o', 10);
System.out.println("Last index of 'o' from index 10: " + lastIndexOfOFromIndex);
}
}
Output:
Last index of 'o' from index 10: 8
Handling Not Found Cases
The lastIndexOf method returns -1 if the specified character or substring is not found.
Example
public class LastIndexOfExample {
public static void main(String[] args) {
String message = "Hello, World!";
int lastIndexOfJava = message.lastIndexOf("Java");
System.out.println("Last index of 'Java': " + lastIndexOfJava);
}
}
Output:
Last index of 'Java': -1
Conclusion
The String.lastIndexOf() method in Java is for locating the position of the last occurrence of characters and substrings within a string. By understanding how to use this method, you can efficiently search, parse, and manipulate strings in your Java applications. Whether you are finding the last index of a character, a substring, starting the search from a specific index, or handling cases where the character or substring is not found, the lastIndexOf method provides a reliable solution for these tasks.
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment