Java – StringBuilder substring() method

In this article, we will discuss StringBuilder’s substring() method which is used to get sub-string (partial string or portion of string from StringBuilder as per specified range)

1. StringBuilder’s substring() method :

  • This StringBuilder method is used to get sub-string 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

1.1 Method Signature:

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

1.4 Throws:

  • substring() method throws StringIndexOutOfBoundsException, if index value passed falls out of range i.e.;
    1. if either start-index or end-index is negative (<0)
    2. if start-index is greater than end-index
    3. if end-index is greater than length()
  • subsequence() method throws IndexOutOfBoundsException, if index value passed falls out of range i.e.;
    1. if either start-index or end-index is negative (<0)
    2. if start-index is greater than end-index
    3. if end-index is greater than length()

2. Examples on substring() method :

2.1 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

2.2 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

2.3 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

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java - String to int conversion in 3 ways
Java - StringBuilder reverse() method