The StringBuilder.lastIndexOf()
method in Java is used to find the index of the last occurrence of a specified substring within a StringBuilder
object. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality, including the overloaded methods.
Table of Contents
- Introduction
lastIndexOf
Method Syntax- Examples
- Finding the Last Index of a Substring
- Finding the Last Index of a Substring from a Specified Position
- Real-World Use Case
- Conclusion
Introduction
The StringBuilder.lastIndexOf()
method is a member of the StringBuilder
class in Java. It allows you to search for the last occurrence of a specified substring within the StringBuilder
object. This method is particularly useful when you need to locate the last appearance of a substring in a mutable sequence of characters.
lastIndexOf() Method Syntax
The StringBuilder
class provides two overloaded lastIndexOf
methods:
lastIndexOf(String str)
lastIndexOf(String str, int fromIndex)
Method 1: lastIndexOf(String str)
The syntax for the first lastIndexOf
method is as follows:
public int lastIndexOf(String str)
- str: The substring to search for.
Method 2: lastIndexOf(String str, int fromIndex)
The syntax for the second lastIndexOf
method is as follows:
public int lastIndexOf(String str, int fromIndex)
- str: The substring to search for.
- fromIndex: The index to start the reverse search from.
Examples
Finding the Last Index of a Substring
The first lastIndexOf
method can be used to find the index of the last occurrence of a specified substring within a StringBuilder
.
Example
public class StringBuilderLastIndexOfExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World! Hello, World!");
// Find the last index of the substring "World"
int lastIndex = sb.lastIndexOf("World");
// Print the index
System.out.println("Last index of 'World': " + lastIndex);
}
}
Output:
Last index of 'World': 20
Finding the Last Index of a Substring from a Specified Position
The second lastIndexOf
method can be used to find the index of the last occurrence of a specified substring, starting the reverse search from a given index.
Example
public class StringBuilderLastIndexOfExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World! Hello, World!");
// Find the last index of the substring "World" before position 15
int lastIndex = sb.lastIndexOf("World", 15);
// Print the index
System.out.println("Last index of 'World' before position 15: " + lastIndex);
}
}
Output:
Last index of 'World' before position 15: 7
In this example, the reverse search for the substring "World" starts from index 15, and it finds the first occurrence of "World" in the string starting from the end and moving backwards.
Real-World Use Case
Example: Finding the Last Occurrence of a File Extension
In a real-world scenario, you might need to find the last occurrence of a file extension in a file path. Using the lastIndexOf
method, you can locate the position of the last dot (.) to extract the file extension.
Example Code
public class FileExtensionFinder {
public static void main(String[] args) {
StringBuilder filePath = new StringBuilder("C:/Users/JohnDoe/Documents/Report.final.docx");
// Find the last index of the dot (.) to locate the file extension
int lastIndex = filePath.lastIndexOf(".");
// Extract the file extension
String fileExtension = filePath.substring(lastIndex + 1);
// Print the file extension
System.out.println("File extension: " + fileExtension);
}
}
Output:
File extension: docx
Conclusion
The StringBuilder.lastIndexOf()
method in Java is used for locating the last occurrence of substrings within a StringBuilder
object. By understanding how to use the overloaded methods, you can efficiently search for substrings either from the end of the StringBuilder
or from a specified position. Whether you need to find the last index of a substring or start the reverse search from a particular index, the lastIndexOf
methods provide a reliable solution for these tasks.