java.lang.StringBuilder subSequence(int start,int end)
Description
sb.subsequence(start, end)
is equivalent to
sb.substring(start, end)
Notes:
- An IndexOutOfBoundsException will be thrown by the subsequence() method if end is greater than length(), or if start is greater than end.
- This method is specified by subSequence in interface CharSequence.
Method Syntax
public CharSequence subSequence(int start,int end)
Method Argument
Data Type | Parameter | Description |
---|---|---|
int | start | the start index, inclusive. |
int | end | the end index, exclusive. |
Method Returns
The subSequence(int start,int end) method returns the specified subsequence.
Compatibility
Requires Java 1.5 and up
Java StringBuilder subSequence(int start,int end) Example
Below is a java code demonstrates the use of subSequence(int start,int end) method of StringBuilder class. The example presented might be simple however it shows the behavior of the subSequence(int start,int end) method.
package com.javatutorialhq.java.examples; /* * A java example source code to demonstrate * the use of subSequence() method of StringBuilder class */ public class StringBuilderSubSequenceExample { public static void main(String[] args) { // initialize the StringBuilder object StringBuilder sb = new StringBuilder("javatutorialhq.com"); System.out.println("Contents of buffer:" + sb); /* * get the CharSequence from this StringBuilder * starting from * index 3 to index 10 */ int start = 3; int end = 10; CharSequence cs = sb.subSequence(start, end); System.out.println("Results:" + cs); } }
The java example source code above, demonstrates the use of subSequence(int start,int end) method of StringBuilder class. Initially the code assigns a string “javatutorialhq.com” as initial contents of the StringBuilder. Then we use the subsequence to get a part of the sequence based from index 3 until the end index 10.