📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 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 (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
StringBuilder.substring()
method in Java is used to return a new string that is a substring of the current sequence. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.Table of Contents
- Introduction
substring
Method Syntax- Examples
- Extracting a Substring
- Extracting a Substring with Only a Starting Index
- Handling IndexOutOfBoundsException
- Conclusion
Introduction
The StringBuilder.substring()
method is a member of the StringBuilder
class in Java. It allows you to extract a portion of the character sequence from the StringBuilder
and return it as a new String
. This method is particularly useful when you need to work with a portion of the character sequence as a standalone string.
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 the first substring
method is as follows:
public String substring(int start)
- start: The starting index, inclusive.
Method 2: substring(int start, int end)
The syntax for the second substring
method is as follows:
public String substring(int start, int end)
- start: The starting index, inclusive.
- end: The ending index, exclusive.
Examples
Extracting a Substring
The first substring
method can be used to extract a portion of the character sequence starting from a given index until the end.
Example
public class StringBuilderSubstringExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Hello, World!"); String subStr = sb.substring(7); System.out.println("Substring: " + subStr); } }
Output:
Substring: World!
Extracting a Substring with Only a Starting Index
The second substring
method can be used to extract a portion of the character sequence between two specified indices.
Example
public class StringBuilderSubstringExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Hello, World!"); String subStr = sb.substring(7, 12); System.out.println("Substring: " + subStr); } }
Output:
Substring: World
Handling IndexOutOfBoundsException
Attempting to use invalid indices will result in an IndexOutOfBoundsException
. It's important to ensure that the specified range is within the valid bounds of the StringBuilder
.
Example
public class StringBuilderSubstringExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Hello, World!"); try { String subStr = sb.substring(7, 20); // This will throw an exception System.out.println("Substring: " + subStr); } catch (IndexOutOfBoundsException e) { System.out.println("Error: " + e.getMessage()); } } }
Output:
Error: start 7, end 20, length 13
Conclusion
The StringBuilder.substring()
method in Java is used for extracting a portion of the character sequence from a StringBuilder
object and returning it as a new string. By understanding how to use the overloaded methods, you can efficiently work with substrings within your StringBuilder
objects. Whether you need to extract a substring or handle potential exceptions, the substring
method provides a reliable solution for these tasks.
Comments
Post a Comment
Leave Comment