In this article, we will discuss StringBuilder’s substring() method which is used to get substring (partial string or portion of string from StringBuilder as per specified range)
substring() method:
- This StringBuilder method is used to get substring for the specified begin-value and end-value
Note:
- there are 2 variants or overloaded substring() methods
- 1st variant returns substring starting from specified index-position till length
- Whereas 2nd variant returns substring starting from specified index-position to specified end index-position
- In addition to this, there is one more StringBuilder method similar to 2nd variant i.e.; subSequence() method
Method Signature:
public String substring(int start); public String substring(int start, int end); public CharSequence subSequence(int start, int end);
Parameters:
- Start –> start index (indicates from where string need to be extracted and it is inclusive)
- end –> end index (indicates till where string need to be extracted and it is exclusive)
Returns:
substring() method |
Returns |
public String substring(int start); | Returns sub-string from the invoking StringBuilder object, starting from specified begin-index |
public String substring(int start, int end); | Returns sub-string from the invoking StringBuilder object, starting from specified begin-index till end-index |
public CharSequence subSequence(int start, int end); | Very similar to substring(beginIndex, endIndex);
Returns char sequence from the invoking StringBuilder object, starting from specified begin-index till end-index |
Throws:
substring() method throws StringIndexOutOfBoundsException, if index value passed falls out of range i.e.;
- if either start-index or end-index is negative (<0)
- if start-index is greater than end-index
- if end-index is greater than length()
subsequence() method throws IndexOutOfBoundsException, if index value passed falls out of range i.e.;
- if either start-index or end-index is negative (<0)
- if start-index is greater than end-index
- if end-index is greater than length()
Examples on substring() method:
Example 1: Java program to get sub-string starting from specified begin-index
Method Signature:
public String substring(int start);
StringBuilderSubstringMethod.java
package in.bench.resources.stringbuilder.methods; public class StringBuilderSubstringMethod { public static void main(String[] args) { // StringBuffer 1: to get substring() - // starting from 10th index-position StringBuffer sb1 = new StringBuffer("DotNet is another"); sb1.append(" Object-oriented programming language"); String subString1 = sb1.substring(10); System.out.println("sb1.substring(10) is : " + subString1); // StringBuffer 2: to get substring() - // starting from 23rd index-position StringBuffer sb2= new StringBuffer("BenchResources.Net "); sb2.append("has over 500+ articles"); // 1st append String subString2 = sb2.substring(23); System.out.println("\nsb2.substring(23) is : " + subString2); // StringBuffer 3: to get substring() - // starting from 17th index-position StringBuffer sb3 = new StringBuffer("String class"); sb3.append(" has useful"); // 1st append sb3.append(" methods"); // 2nd append String subString3 = sb3.substring(17); System.out.println("\nsb3.substring(17) is : " + subString3); } }
Output:
sb1.substring(10) is : another Object-oriented programming language sb2.substring(23) is : over 500+ articles sb3.substring(17) is : useful methods
Example 2: Java program to get sub-string starting from specified begin-index till specified end-index
Method Signature:
public String substring(int start, int end);
StringBuilderSubstringMethod2.java
package in.bench.resources.stringbuilder.methods; public class StringBuilderSubstringMethod2 { public static void main(String[] args) { // StringBuilder 1: to get substring() - // starting from 10th till 33rd index-position StringBuilder sb1= new StringBuilder("DotNet is another"); sb1.append(" Object-oriented programming language"); String subString1 = sb1.substring(10, 33); System.out.println("sb1.substring(10, 33) is : " + subString1); // StringBuilder 2: to get substring() - // starting from 28th till 41st index-position StringBuilder sb2 = new StringBuilder( "BenchResources.Net "); sb2.append("has over 500+ articles"); // 1st append String subString2 = sb2.substring(28, 41); System.out.println("\nsb2.substring(28, 41) is : " + subString2); // StringBuilder 3: to get substring() - // starting from 07th till 23rd index-position StringBuilder sb3 = new StringBuilder("String class"); sb3.append(" has useful"); // 1st append sb3.append(" methods"); // 2nd append String subString3 = sb3.substring(07, 23); System.out.println("\nsb3.substring(07, 23) is : " + subString3); } }
Output:
sb1.substring(10, 33) is : another Object-oriented sb2.substring(28, 41) is : 500+ articles sb3.substring(07, 23) is : class has useful
Example 3: Java program to get char sequence starting from specified begin-index till specified end-index
- Note: very similar to substring(beginIndex, endIndex);
Method Signature:
public CharSequence subSequence(int start, int end);
StringBuilderSubsequenceMethod.java
package in.bench.resources.stringbuilder.methods; public class StringBuilderSubsequenceMethod { public static void main(String[] args) { // StringBuilder 1: to get subSequence() - // starting from 10th till 33rd index-position StringBuilder sb1 = new StringBuilder( "DotNet is another"); sb1.append(" Object-oriented programming language"); CharSequence charSequence1 = sb1.subSequence(10, 33); System.out.println("sb1.subSequence(10, 33) is : " + charSequence1); // StringBuilder 2: to get subSequence() - // starting from 28th till 41st index-position StringBuilder sb2 = new StringBuilder( "BenchResources.Net "); sb2.append("has over 500+ articles"); // 1st append CharSequence charSequence2 = sb2.subSequence(28, 41); System.out.println("\nsb2.subSequence(28, 41) is : " + charSequence2); // StringBuilder 3: to get subSequence() - // starting from 07th till 23rd index-position StringBuilder sb3 = new StringBuilder( "String class"); sb3.append(" has useful"); // 1st append sb3.append(" methods"); // 2nd append CharSequence charSequence3 = sb3.subSequence(07, 23); System.out.println("\nsb3.subSequence(07, 23) is : " + charSequence3); } }
Output:
sb1.subSequence(10, 33) is : another Object-oriented sb2.subSequence(28, 41) is : 500+ articles sb3.subSequence(07, 23) is : class has useful
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/6/docs/api/java/lang/StringBuilder.html
- https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!