The StringBuilder.substring()
method in Java is used to extract a substring from a StringBuilder
object.
Table of Contents
- Introduction
substring
Method Syntax- Examples
- Extracting a Substring
- Extracting a Substring to the End
- Real-World Use Case
- Conclusion
Introduction
The StringBuilder.substring()
method is a member of the StringBuilder
class in Java. It allows you to extract a specific portion of the character sequence contained in the StringBuilder
object. This method is useful when you need a part of the sequence as a new String
object without modifying the original StringBuilder
.
substring() Method Syntax
The StringBuilder
class provides two overloaded substring
methods:
substring(int start)
substring(int start, int end)
Method 1: substring(int start)
The syntax for extracting a substring from the specified start index to the end of the StringBuilder
is as follows:
public String substring(int start)
- start: The beginning index, inclusive.
Method 2: substring(int start, int end)
The syntax for extracting a substring from the specified start index to the specified end index is as follows:
public String substring(int start, int end)
- start: The beginning index, inclusive.
- end: The ending index, exclusive.
Examples
Extracting a Substring
The substring
method can be used to extract a substring within a specified range from a StringBuilder
.
Example
public class StringBuilderSubstringExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World!");
// Extract a substring from index 7 to 12
String subStr = sb.substring(7, 12);
// Print the result
System.out.println("Substring: " + subStr);
}
}
Output:
Substring: World
Extracting a Substring to the End
You can also extract a substring from a specified start index to the end of the StringBuilder
.
Example
public class StringBuilderSubstringExample {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, World!");
// Extract a substring from index 7 to the end
String subStr = sb.substring(7);
// Print the result
System.out.println("Substring: " + subStr);
}
}
Output:
Substring: World!
Real-World Use Case
Example: Extracting File Extensions
In a real-world scenario, you might need to extract the file extension from a file name. Using the substring
method, you can locate the position of the last dot and extract the extension.
Example Code
public class FileExtensionExtractor {
public static void main(String[] args) {
StringBuilder fileName = new StringBuilder("example.document.txt");
// Find the position of the last dot
int lastDotIndex = fileName.lastIndexOf(".");
// Extract the file extension
String fileExtension = fileName.substring(lastDotIndex + 1);
// Print the file extension
System.out.println("File extension: " + fileExtension);
}
}
Output:
File extension: txt
Conclusion
The StringBuilder.substring()
method in Java is used for extracting substrings from a StringBuilder
object. By understanding how to use the overloaded methods, you can efficiently extract specific portions of a character sequence as new String
objects. Whether you need to extract a substring within a specified range or from a start index to the end, the substring
method provides a reliable solution for these tasks.