GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — StringBuffer subSequence() in Java with Examples - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

StringBuffer subSequence() in Java with Examples

Last Updated : 17 Feb, 2022

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:

  1. start which is Integer type value refers to the start index of subsequence.
  2. 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

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.