In this article, we will discuss StringBuffer’s substring() method which is used to get substring (partial string or portion of string from StringBuffer as per specified range)
1. StringBuffer’s substring() method:
- This StringBuffer method is used to get sub-string for the specified begin-indexand end-index
- There are 2 variants or overloaded substring() method,
- 1st variant returns substring starting from specified index-position till length
- 2nd variant returns substring starting from specified index-position to specified end index-position
- In addition to this, there is one more StringBuffer method similar to 2nd variant i.e.; subSequence() method
1.1 Method Signature:
1 2 3 4 5 | public String substring( int start); public String substring( int start, int end); public CharSequence subSequence( int start, int end); |
1.2 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)
1.3 Returns:
substring() method |
Returns |
public String substring(int start); | Returns substring from the invoking StringBuffer object, starting from specified begin-index |
public String substring(int start, int end); | Returns substring from the invoking StringBuffer 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 StringBuffer object, starting from specified begin-index till end-index |
1.4 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()
2. Examples on substring() method:
2.1 To get sub-string starting from specified begin-index
Method Signature:
1 | public String substring( int start); |
StringBufferSubstringMethod.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package in.bench.resources.stringbuffer.methods; public class StringBufferSubstringMethod { public static void main(String[] args) { // StringBuffer 1: to get substring() - // starting from 10th index-position StringBuffer sb1 = new StringBuffer( "Java is a super cool language" ); String subString1 = sb1.substring( 10 ); System.out.println( "sb1.substring(10) is : " + subString1); // StringBuffer 2: to get substring() - // starting from 24th index-position StringBuffer sb2 = new StringBuffer( "BenchResources.Net " ); sb2.append( "is a Java weblog" ); // 1st append String subString2 = sb2.substring( 24 ); System.out.println( "\nsb2.substring(24) is : " + subString2); // StringBuffer 3: to get substring() - // starting from 13th index-position StringBuffer sb3 = new StringBuffer( "String class " ); sb3.append( "has useful " ); // 1st append sb3.append( "methods" ); // 2nd append String subString3 = sb3.substring( 13 ); System.out.println( "\nsb3.substring(13) is : " + subString3); } } |
Output:
1 2 3 4 5 | sb1.substring( 10 ) is : super cool language sb2.substring( 24 ) is : Java weblog sb3.substring( 13 ) is : has useful methods |
2.2 To get substring starting from specified begin-index till specified end-index
Method Signature:
1 | public String substring( int start, int end); |
StringBufferSubstringMethod2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package in.bench.resources.stringbuffer.methods; public class StringBufferSubstringMethod2 { public static void main(String[] args) { // StringBuffer 1: to get substring() - // starting from 10th till 20th index-position StringBuffer sb1 = new StringBuffer( "Java is a super cool language" ); String subString1 = sb1.substring( 10 , 20 ); System.out.println( "sb1.substring(10, 20) is : " + subString1); // StringBuffer 2: to get substring() - // starting from 24th till 28th index-position StringBuffer sb2 = new StringBuffer( "BenchResources.Net " ); sb2.append( "is a Java weblog" ); // 1st append String subString2 = sb2.substring( 24 , 28 ); System.out.println( "\nsb2.substring(24, 28) is : " + subString2); // StringBuffer 3: to get substring() - // starting from 7th till 23rd index-position StringBuffer sb3 = new StringBuffer( "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:
1 2 3 4 5 | sb1.substring( 10 , 20 ) is : super cool sb2.substring( 24 , 28 ) is : Java sb3.substring( 07 , 23 ) is : class has useful |
2.3 To get char sequence starting from specified begin-index till specified end-index
- Note: very similar to substring(beginIndex, endIndex);
Method Signature:
1 | public CharSequence subSequence( int start, int end); |
StringBufferSubsequenceMethod.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package in.bench.resources.stringbuffer.methods; public class StringBufferSubsequenceMethod { public static void main(String[] args) { // Example 1: to get subSequence() - // starting from 10th till 20th index-position StringBuffer sb1 = new StringBuffer( "Java is a super cool language" ); CharSequence charSequence1 = sb1.substring( 10 , 20 ); System.out.println( "sb1.subSequence(10, 20) is : " + charSequence1); // Example 2: to get subSequence() - // starting from 24th till 28th index-position StringBuffer sb2 = new StringBuffer( "BenchResources.Net " ); sb2.append( "is a Java weblog" ); // 1st append CharSequence charSequence2 = sb2.subSequence( 24 , 28 ); System.out.println( "\nsb2.subSequence(24, 28) is : " + charSequence2); // Example 3: to get subSequence() - // starting from 7th till 23rd index-position StringBuffer sb3 = new StringBuffer( "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:
1 2 3 4 5 | sb1.subSequence( 10 , 20 ) is : super cool sb2.subSequence( 24 , 28 ) is : Java sb3.subSequence( 07 , 23 ) is : class has useful |
Related Articles:
- Java – StringBuffer class
- Java – StringBuffer append() method (13)
- Java – StringBuffer capacity() method
- Java – StringBuffer charAt(int index) method
- Java – StringBuffer delete(int start, int end) method
- Java – StringBuffer deleteCharAt(int index) method
- Java – StringBuffer ensureCapacity(int minimumCapacity) method
- Java – StringBuffer indexOf() method (2)
- Java – StringBuffer insert() method (12)
- Java – StringBuffer lastIndexOf() method (2)
- Java – StringBuffer length() method
- Java – StringBuffer replace(int start, int end, String str) method
- Java – StringBuffer reverse() method
- Java – StringBuffer substring(int start, int end) method
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/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!