The String.lastIndexOf()
method in Java is used to find the index of the last occurrence of a specified character or substring within a string.
Table of Contents
- Introduction
lastIndexOf
Method Syntax- Examples
- Finding the Last Occurrence of a Character
- Finding the Last Occurrence of a Substring
- Finding with a Start Index
- Handling Cases Where the Character or Substring is Not Found
- Real-World Use Case
- Conclusion
Introduction
The String.lastIndexOf()
method is a member of the String
class in Java. It allows you to find the position of the last occurrence of a character or substring within a string. If the character or substring is found, the method returns the index of its last occurrence. If it is not found, the method returns -1
.
lastIndexOf() Method Syntax
The syntax for the lastIndexOf
method is as follows:
Finding the Last Occurrence of a Character
public int lastIndexOf(int ch)
Finding the Last Occurrence of a Character from a Specified Index
public int lastIndexOf(int ch, int fromIndex)
Finding the Last Occurrence of a Substring
public int lastIndexOf(String str)
Finding the Last Occurrence of a Substring from a Specified Index
public int lastIndexOf(String str, int fromIndex)
Examples
Finding the Last Occurrence of a Character
The lastIndexOf
method can be used to find the last occurrence of a character within a string.
Example
public class LastIndexOfCharExample {
public static void main(String[] args) {
String str = "Welcome to Java Programming";
int index = str.lastIndexOf('a');
System.out.println("Last index of 'a': " + index);
}
}
Output:
Last index of 'a': 23
Finding the Last Occurrence of a Substring
The lastIndexOf
method can be used to find the last occurrence of a substring within a string.
Example
public class LastIndexOfSubstringExample {
public static void main(String[] args) {
String str = "Welcome to Java Programming";
int index = str.lastIndexOf("Java");
System.out.println("Last index of 'Java': " + index);
}
}
Output:
Last index of 'Java': 11
Finding with a Start Index
The lastIndexOf
method can also be used to find the last occurrence of a character or substring, starting the search from a specified index.
Finding the Last Occurrence of a Character from a Specified Index
public class LastIndexOfCharFromIndexExample {
public static void main(String[] args) {
String str = "Welcome to Java Programming";
int index = str.lastIndexOf('a', 10);
System.out.println("Last index of 'a' from index 10: " + index);
}
}
Output:
Last index of 'a' from index 10: 9
Finding the Last Occurrence of a Substring from a Specified Index
public class LastIndexOfSubstringFromIndexExample {
public static void main(String[] args) {
String str = "Welcome to Java Programming, Welcome to Learning";
int index = str.lastIndexOf("Welcome", 30);
System.out.println("Last index of 'Welcome' from index 30: " + index);
}
}
Output:
Last index of 'Welcome' from index 30: 0
Handling Cases Where the Character or Substring is Not Found
If the character or substring is not found, the lastIndexOf
method returns -1
.
Example
public class LastIndexOfNotFoundExample {
public static void main(String[] args) {
String str = "Welcome to Java Programming";
int index = str.lastIndexOf('z');
System.out.println("Last index of 'z': " + index);
}
}
Output:
Last index of 'z': -1
Real-World Use Case
Example: Finding the Last Occurrence of a File Extension
One common use case for lastIndexOf
is finding the position of the last occurrence of a file extension in a file path.
public class FileExtensionExample {
public static void main(String[] args) {
String filePath = "/home/user/documents/report.pdf";
int index = filePath.lastIndexOf('.');
if (index != -1) {
String extension = filePath.substring(index);
System.out.println("File extension: " + extension);
} else {
System.out.println("No file extension found.");
}
}
}
Output:
File extension: .pdf
In this example, the lastIndexOf
method is used to find the position of the last occurrence of the dot (.
) character to extract the file extension.
Conclusion
The String.lastIndexOf()
method in Java is used for finding the index of the last occurrence of a character or substring within a string. It provides various overloads to accommodate different use cases, including starting the search from a specific index. This method is particularly useful for searching, validation, and manipulation tasks in various applications. By understanding and utilizing the lastIndexOf
method, you can efficiently manage string searches in your Java programs.