Java StringBuilder.substring() – Examples
In this tutorial, we will learn about the Java StringBuilder.substring() function, and learn how to use this function with the help of examples.
substring(int start)
StringBuilder.substring() returns a new String that contains a subsequence of characters, formed using start position and an optional end position, contained in this character sequence.
Syntax
The syntax of substring() function is
substring(int start)
where
Parameter | Description |
---|---|
start | Integer representing the start position of substring in StringBuilder. |
Returns
The function returns String object.
Example 1 – substring(int start)
In this example, we will create a StringBuilder object and initialize with some string passed as argument.
We will then get the sub-string from the StringBuilder using substring() method from starting position of 5 in the character sequence of StringBuilder.
Java Program
class Example { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder stringBuilder = new StringBuilder("abcdefghijkl"); // print string System.out.println("Original String: "+stringBuilder); // get substring start from index 5 // using substring() and print results System.out.println("Sub String : "+ stringBuilder.substring(5)); } }
Output
Original String: abcdefghijkl Sub String : fghijkl
Example 2 – substring(int start)
In this example, we will test a negative scenario where substring() throws java.lang.StringIndexOutOfBoundsException. We will give an index that is out of range for the sequence in StringBuilder.
Java Program
class Example { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("abcdefghijklm"); try { // get substring starts from index -7 str.substring(-7); } catch (Exception e) { System.out.println("Exception: " + e); } } }
Output
Exception: java.lang.StringIndexOutOfBoundsException: start -7, end 13, length 13
substring(int start, int end)
StringBuilder.substring() returns a new String that contains a subsequence of characters currently contained in this sequence, whose starting and ending are provided as arguments.
Syntax
The syntax of substring() function is
substring(int start, int end)
where
Parameter | Description |
---|---|
start | Integer representing the start position of substring in StringBuilder. |
end | Integer representing the end position of substring in StringBuilder. |
Returns
The function returns String object.
Example 3 – substring(int start, int end)
In this example, we will create a StringBuilder object and initialize with some string passed as argument.
We will then get the sub-string from the StringBuilder using substring() method from starting position of 5 and ending position of 8 in the character sequence of StringBuilder
Java Program
class Example { public static void main(String[] args) { StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn"); // print string System.out.println("Original String : " + stringBuilder); // get substring start from index 5 to index 8 // using substring() and print results System.out.println("Sub String : "+ stringBuilder.substring(5, 8)); } }
Output
Original String : abcdefghijklmn Sub String : fgh
Example 4 – substring(int start, int end)
In this example, we will test a negative scenario where substring() throws java.lang.StringIndexOutOfBoundsException. We will give a value for start
parameter such that it is greater than end
.
Java Program
class Example { public static void main(String[] args) { StringBuilder stringBuilder = new StringBuilder("abcdefghijkl"); System.out.println("Original String: "+stringBuilder); System.out.println("Sub String : "+ stringBuilder.substring(8, 2)); } }
Output
Original String: abcdefghijkl Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start 8, end 2, length 12 at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(Unknown Source) at java.base/java.lang.AbstractStringBuilder.substring(Unknown Source) at java.base/java.lang.StringBuilder.substring(Unknown Source) at Example.main(Example.java:12)
Conclusion
In this Java Tutorial, we have learnt the syntax of Java StringBuilder.substring() function, and also learnt how to use this function with the help of examples.