Java StringBuffer subSequence() Method



The Java StringBuffer subSequence() method is used to retrieve a subsequence from a StringBuffer 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 StringBuffer 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 StringBuffer 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.

public class SubSequence {
   public static void main(String[] args) {
      //instantiate the StringBuffer class
      StringBuffer sb = new StringBuffer("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 StringBuffer 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.

public class SubSequence {
   public static void main(String[] args) {
      try {
         //create an object of the StringBuffer class
         StringBuffer sb = new StringBuffer("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: Range [-1, -2) out of bounds for length 15
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
   at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
   at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
   at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
   at java.base/java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:1057)
   at java.base/java.lang.StringBuffer.subSequence(StringBuffer.java:516)
   at SubSequence.main(SubSequence.java:15)
Exception: java.lang.StringIndexOutOfBoundsException: Range [-1, -2) out of bounds for length 15

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

public class SubSequence {
   public static void main(String[] args) {
      try {
         //instantiate the StringBuffer class
         StringBuffer sb = new StringBuffer("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: Range [2, 25) out of bounds for length 11
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
   at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
   at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
   at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
   at java.base/java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:1057)
   at java.base/java.lang.StringBuffer.subSequence(StringBuffer.java:516)
   at SubSequence.main(SubSequence.java:15)
Exception: java.lang.StringIndexOutOfBoundsException: Range [2, 25) out of bounds for length 11
java_lang_stringbuffer.htm
Advertisements