🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
StringBuffer.substring() method in Java is used to retrieve a substring from the StringBuffer object. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
substringMethod Syntax- Examples
- Retrieving a Substring from a Start Index
- Retrieving a Substring from a Start Index to an End Index
- Conclusion
Introduction
The substring() method is a member of the StringBuffer class in Java. It allows you to extract a substring from the StringBuffer and returns it as a new String. This is useful for operations that require working with specific portions of the character sequence without modifying the original StringBuffer.
substring Method Syntax
The syntax for the substring method is as follows:
public synchronized String substring(int start)
Parameters:
start- the starting index (inclusive) of the substring.
Returns:
- A new
Stringthat contains the subsequence of characters from the specified start index to the end of the sequence.
Throws:
IndexOutOfBoundsException- ifstartis negative or greater than the length of this sequence.
public synchronized String substring(int start, int end)
Parameters:
start- the starting index (inclusive) of the substring.end- the ending index (exclusive) of the substring.
Returns:
- A new
Stringthat contains the subsequence of characters from the specified start index to the specified end index.
Throws:
IndexOutOfBoundsException- ifstartorendare negative, ifstartis greater thanend, or ifendis greater than the length of this sequence.
Examples
Retrieving a Substring from a Start Index
The substring method can be used to extract a substring from a specified start index to the end of the StringBuffer.
Example
public class StringBufferSubstringExample {
public static void main(String[] args) {
// Create a StringBuffer object with initial content
StringBuffer sb = new StringBuffer("Hello, World!");
// Retrieve a substring from index 7 to the end
String subStr = sb.substring(7);
// Print the substring
System.out.println("Substring: " + subStr);
}
}
Output:
Substring: World!
Retrieving a Substring from a Start Index to an End Index
The substring method can also be used to extract a substring from a specified start index to a specified end index.
Example
public class StringBufferSubstringExample {
public static void main(String[] args) {
// Create a StringBuffer object with initial content
StringBuffer sb = new StringBuffer("Hello, World!");
// Retrieve a substring from index 7 to 12
String subStr = sb.substring(7, 12);
// Print the substring
System.out.println("Substring: " + subStr);
}
}
Output:
Substring: World
Handling Edge Cases
It is important to handle cases where the specified indices are out of bounds or invalid.
Example
public class StringBufferSubstringExample {
public static void main(String[] args) {
// Create a StringBuffer object with initial content
StringBuffer sb = new StringBuffer("Hello, World!");
try {
// Attempt to retrieve a substring with an invalid range
String subStr = sb.substring(12, 7);
} catch (IndexOutOfBoundsException e) {
// Handle the exception
System.out.println("Error: " + e.getMessage());
}
try {
// Attempt to retrieve a substring with an end index greater than the length
String subStr = sb.substring(7, 20);
} catch (IndexOutOfBoundsException e) {
// Handle the exception
System.out.println("Error: " + e.getMessage());
}
try {
// Attempt to retrieve a substring with a negative start index
String subStr = sb.substring(-1, 5);
} catch (IndexOutOfBoundsException e) {
// Handle the exception
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Error: start 12, end 7
Error: end 20
Error: start -1
Conclusion
The StringBuffer.substring() method in Java provides a way to extract a substring from a StringBuffer object and return it as a new String. By understanding how to use this method, you can efficiently work with specific portions of the character sequence without modifying the original StringBuffer. This method is particularly useful for applications that require read-only access to subsequences of the character data.
Comments
Post a Comment
Leave Comment