Java: StringBuffer substring() method

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)

 

substring() method:

  • This StringBuffer method is used to get substring for the specified begin-indexand end-index

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 StringBuffer 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 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

 

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 substring starting from specified begin-index

Method Signature:

public String substring(int start);

 

StringBufferSubstringMethod.java

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:

sb1.substring(10) is : super cool language

sb2.substring(24) is : Java weblog

sb3.substring(13) is : has useful methods

 

Example 2: Java program to get substring starting from specified begin-index till specified end-index

Method Signature:

public String substring(int start, int end);

 

StringBufferSubstringMethod2.java

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:

sb1.substring(10, 20) is : super cool

sb2.substring(24, 28) is : Java

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);

 

StringBufferSubsequenceMethod.java

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:

sb1.subSequence(10, 20) is : super cool

sb2.subSequence(24, 28) is : Java

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 !!

Java: StringBuilder class
Java: StringBuffer reverse() method