Java - StringBuilder subSequence() Method



The Java StringBuilder subSequence() method is used to retrieve a subsequence from a StringBuilder object. A sub-sequence is small part of a string or a sequence.

The subsequence() method accepts two parameters as an integer that holds the start and end index values. It throws an exception if the start index or end index values are negative, the start index value is greater than the end index value, and the end index value is greater than the sequence length.

Syntax

Following is the syntax of the Java StringBuilder subSequence() method −

public CharSequence subSequence(int start, int end)

Parameters

  • start − This is the beginning index, inclusive.

  • end − This is the ending index, exclusive

Return Value

This method returns the specified subsequence.

Example

If the start index and end index values are positive and less than the sequence length, the subsequence() method returns the subsequence of the given sequence.

In the following program, we are instantiating the StringBuilder class with the value of “Java Programming”. Then, using the subsequence() method, we are trying to retrieve the sub-sequence of the given sequence at the specified start index 5, and end index 16.

package com.tutorialspoint.StringBuilder;
public class SubSequence {
   public static void main(String[] args) {
      
      //instantiate the StringBuilder class
      StringBuilder sb = new StringBuilder("Java Programming");
      System.out.println("The string is: " + sb.toString());
      
      //initialize the startIndex and endIndex values
      int startIndex  = 5;
      int endIndex = 16;
      System.out.println("The initialize values of the startIndex and endIndex are: " + startIndex + " and " + endIndex);
      
      //using the subSequence() method
      System.out.println("The sub-sequence of the given sequence is: " + sb.subSequence(startIndex, endIndex));
   }
}

Output

On executing the above program, it will produce the following result −

The string is: Java Programming
The initialize values of the startIndex and endIndex are: 5 and 16
The sub-sequence of the given sequence is: Programming

Example

If the given start index and end index values are negative, this method throws an StringIndexOutOfBoundsException.

In the following program, we are creating an object of the StringBuilder class with the value of “Tutorials Point”. Using the subsequence() method, we are trying to retrieve the sub-sequence of the given sequence at the specified start index -1, and end index -2.

package com.tutorialspoint.StringBuilder;
public class SubSequence {
   public static void main(String[] args) {
      try {
         
         //create an object of the StringBuilder class
         StringBuilder sb = new StringBuilder("Tutorials Point");
         System.out.println("The string is: " + sb.toString());
         
         //initialize the startIndex and endIndex values
         int startIndex  = -1;
         int endIndex = -2;
         System.out.println("The initialize values of the startIndex and endIndex are: " + startIndex + " and " + endIndex);
         
         //using the subSequence() method
         System.out.println("The sub-sequence of the given sequence is: " + sb.subSequence(startIndex, endIndex));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

The string is: Tutorials Point
The initialize values of the startIndex and endIndex are: -1 and -2
java.lang.StringIndexOutOfBoundsException: start -1, end -2, length 15
	at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(AbstractStringBuilder.java:1810)
	at java.base/java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:1070)
	at java.base/java.lang.StringBuilder.substring(StringBuilder.java:91)
	at java.base/java.lang.AbstractStringBuilder.subSequence(AbstractStringBuilder.java:1052)
	at java.base/java.lang.StringBuilder.subSequence(StringBuilder.java:91)
Exception: java.lang.StringIndexOutOfBoundsException: start -1, end -2, length 15
	at com.tutorialspoint.StringBuilder.SubSequence.main(SubSequence.java:13)

Example

If the end index value is greater than the sequence length, the subsequence() method throws an IndexOutOfBoundException.

In this program, we are instantiating the StringBuilder class with the value

“Hello World”

. Using the subsequence() method, we are trying to retrieve the sub-sequence of the given sequence at the specified start index of 5, and end index of 20.

package com.tutorialspoint.StringBuilder;
public class SubSequence {
   public static void main(String[] args) {
      try {
         
         //instantiate the StringBuilder class
         StringBuilder sb = new StringBuilder("Hello World");
         System.out.println("The string is: " + sb.toString());
         
         //initialize the startIndex and endIndex values
         int startIndex  = 2;
         int endIndex = 25;
         System.out.println("The initialize values of the startIndex and endIndex are: " + startIndex + " and " + endIndex);
         
         //using the subSequence() method
         System.out.println("The sub-sequence of the given sequence is: " + sb.subSequence(startIndex, endIndex));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

The above program, produces the following results −

The string is: Hello World
The initialize values of the startIndex and endIndex are: 2 and 25
java.lang.StringIndexOutOfBoundsException: start 2, end 25, length 11
	at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(AbstractStringBuilder.java:1810)
	at java.base/java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:1070)
	at java.base/java.lang.StringBuilder.substring(StringBuilder.java:91)
	at java.base/java.lang.AbstractStringBuilder.subSequence(AbstractStringBuilder.java:1052)
	at java.base/java.lang.StringBuilder.subSequence(StringBuilder.java:91)
	at com.tutorialspoint.StringBuilder.SubSequence.main(SubSequence.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: start 2, end 25, length 11
java_lang_stringbuilder.htm
Advertisements