StringBuffer subSequence() in Java with Examples Last Updated : 17 Feb, 2022 Comments Improve Suggest changes Like Article Like Report The subSequence(int start, int end) method of StringBuffer 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 index of subsequence.end which is Integer type value refers to the last index 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.StringBuffer.subSequence() method: Example 1: Java // Java program to demonstrate // the subSequence() Method. class GFG { public static void main(String[] args) { // create a StringBuffer object // with a String pass as parameter StringBuffer str = new StringBuffer("WelcomeGeeks"); // print string System.out.println("String contains = " + str); // get subSequence between 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 // Java program to demonstrate // the subSequence() Method. class GFG { public static void main(String[] args) { // create a StringBuffer object // with a String pass as parameter StringBuffer str = new StringBuffer("Indian Team Played Well"); // print string System.out.println("String contains = " + str); // get subSequence between 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 // Java program to demonstrate // Exception thrown by the subSequence() Method. class GFG { public static void main(String[] args) { // create a StringBuffer object // with a String pass as parameter StringBuffer str = new StringBuffer("Indian Team Played Well"); try { // get subSequence between 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.AbstractStringBuffer.substring(AbstractStringBuffer.java:935) at java.lang.StringBuffer.substring(StringBuffer.java:76) at java.lang.AbstractStringBuffer.subSequence(AbstractStringBuffer.java:912) at java.lang.StringBuffer.subSequence(StringBuffer.java:76) at GFG.main(File.java:16) References: https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#subSequence(int, int) Comment More infoAdvertise with us Next Article StringBuffer subSequence() in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java java-basics Java-lang package Java-Functions java-StringBuffer +1 More Practice Tags : Java Similar Reads 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 2 min read Java String subSequence() Method with Examples In Java, the String class provides the subSequence() method. This subSequence() method extracts a portion of a string as a CharSequence. This method is useful for retrieving specific character ranges from a string which enhances string manipulation. In this article, we will learn how to use the subS 3 min read StringBuffer substring() method in Java with Examples In StringBuffer class, there are two types of substring method depending upon the parameters passed to it. substring(int start) The substring(int start) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to end of this sequence. The strin 4 min read StringBuffer toString() method in Java with Examples The toString() method of StringBuffer class is the inbuilt method used to returns a string representing the data contained by StringBuffer Object. A new String object is created and initialized to get the character sequence from this StringBuffer object and then String is returned by toString(). Sub 2 min read ShortBuffer slice() method in Java with Examples The slice() method of java.nio.ShortBuffer Class is used to create a new short buffer whose content is a shared subsequence of this buffer's content. The content of the new buffer will start at this buffer's current position. Changes to this buffer's content will be visible in the new buffer, and vi 3 min read StringBuilder delete() in Java with Examples The delete(int start, int end) method of StringBuilder class removes the characters starting from index start to index end-1 from String contained by StringBuilder. This method takes two indexes as a parameter first start represents index of the first character and endIndex represents index after th 3 min read StringBuffer delete() Method in Java with Examples The java.lang.StringBuffer.delete() is an inbuilt method in Java which is used to remove or delete the characters in a substring of this sequence. The substring starts at a specified index start_point and extends to the character at the index end_point. Syntax : public StringBuffer delete(int start_ 3 min read StringBuffer indexOf() method in Java with Examples In StringBuffer class, there are two types of indexOf() method depending upon the parameters passed to it. indexOf(String str) The indexOf(String str) method of StringBuffer class is used to return the index of the String for first occurrence of passed substring as parameter from the sequence contai 4 min read StringBuffer append() Method in Java with Examples Pre-requisite: StringBuffer class in Java The java.lang.StringBuffer.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used: StringBuffer append(boolean a) :The java.lang.StringBuffer.append(boole 13 min read StringBuffer reverse() Method in Java with Examples The Java.lang.StringBuffer.reverse() is an inbuilt method that is used to reverse the characters in the StringBuffer. The method causes this character sequence to be replaced by the reverse of the sequence. Syntax: public StringBuffer reverse() Parameters: NA Return Value: The method returns the Str 2 min read Like