StringBuilder subSequence() in Java with Examples
The subSequence(int start, int end) method of StringBuilder class is the inbuilt method used to return a subsequence of characters lie between index start and end-1 of this sequence. The subsequence starts with the char value at the index start and ends with the char value at (end-1). The length of the returned subsequence is end-start. So if start is equal to end then an empty subsequence is returned.
Syntax:
public CharSequence subSequence?(int start, int end)
Parameters:
This method accepts two parameters:
- start which is Integer type value refers to the start endex of subsequence.
- end which is Integer type value refers to the last endex of subsequence.
Returns:
This method returns the specified subsequence in range start to end-1.
Exception:
if start or end are negative, if end is greater than length(), or if start is greater than end then IndexOutOfBoundsException is thrown.
Below programs illustrate the java.lang.StringBuilder.subSequence() method:
Example 1:
// Java program to demonstrate // the subSequence() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("WelcomeGeeks"); // print string System.out.println("String contains = " + str); // get subSequence bewteen index 0 to 7 // using subSequence() and print System.out.println("SubSequence = " + str.subSequence(0, 7)); } } |
Output:
String length = 12 and contains = WelcomeGeeks SubSequence = Welcome
Example 2:
// Java program to demonstrate // the subSequence() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("Indian Team Played Well"); // print string System.out.println("String contains = " + str); // get subSequence bewteen index 0 to 7 // using subSequence() and print System.out.println("SubSequence = " + str.subSequence(7, 18)); } } |
Output:
String contains = Indian Team Played Well SubSequence = Team Played
Example 3: When start > end:
// Java program to demonstrate // Exception thrown by the subSequence() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("Indian Team Played Well"); try { // get subSequence bewteen index 0 to 7 // using subSequence() and print System.out.println(str.subSequence(19, 18)); } catch (Exception e) { e.printStackTrace(); } } } |
Output:
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:935)
at java.lang.StringBuilder.substring(StringBuilder.java:76)
at java.lang.AbstractStringBuilder.subSequence(AbstractStringBuilder.java:912)
at java.lang.StringBuilder.subSequence(StringBuilder.java:76)
at GFG.main(File.java:16)
References:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#subSequence(int, int)
Recommended Posts:
- StringBuilder ensureCapacity() in Java with Examples
- StringBuilder setLength() in Java with Examples
- StringBuilder codePointAt() in Java with Examples
- StringBuilder delete() in Java with Examples
- StringBuilder charAt() in Java with Examples
- StringBuilder codePointCount() in Java with Examples
- StringBuilder setCharAt() in Java with Examples
- StringBuilder reverse() in Java with Examples
- StringBuilder Class in Java with Examples
- StringBuilder deleteCharAt() in Java with Examples
- StringBuilder length() in Java with Examples
- StringBuilder getChars() in Java with Examples
- StringBuilder replace() in Java with Examples
- StringBuilder capacity() in Java with Examples
- StringBuilder codePointBefore() in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai



