BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java StringBuffer subSequence()

By Chaitanya Singh | Filed Under: java

Java StringBuffer subSequence() method returns a sub sequence based on the start and end indexes. As we know, StringBuffer is a char sequence, the subSequence() method returns a subset of this sequence.

Syntax of subSequence() method

//returns the char sequence from index 3 to 6
//3 is inclusive and 6 is exclusive
CharSequence cs = sb.subSequence(3, 6);

Here, sb is an object of StringBuffer class.

subSequence() Description

public CharSequence subSequence(int start, int end): It returns a subsequence from the existing StringBuffer sequence using the start and end indexes.

subSequence() Parameters

This method takes two parameters:

  • start: It is an integer index that represents the start of the subsequence. It is inclusive.
  • end: Integer index that represents the end of the subsequence. It is exclusive

subSequence() Return Value

  • It returns a new char sequence which is a subset of the old char sequence.

It throws IndexOutOfBoundsException, if any of the following condition occurs:

  • start < 0 or end <0
  • end >= sb.length()
  • start > end

Example 1: Get subsequence from an existing sequence

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Good luck");
    System.out.println("Sequence: "+sb);

    //getting sub sequence from index 5 to 9
    CharSequence cs = sb.subSequence(5, 9);
    System.out.println("Sub sequence: "+cs);
  }
}

Output:

Java StringBuffer subSequence() Output1

Example 2: Exclude first and last character from sequence

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Text");
    System.out.println("Given Sequence: "+sb);

    //from 2nd char till 2nd last char
    CharSequence cs = sb.subSequence(1, sb.length()-1);
    System.out.println("Sub sequence: "+cs);
  }
}

Output:

Java StringBuffer subSequence() Output2

Example 3: If start index is greater than end index

public class JavaExample {
  public static void main(String[] args) {
    StringBuffer sb = new StringBuffer("Text");
    System.out.println("Given Sequence: "+sb);

    //if start index > end index
    CharSequence cs = sb.subSequence(2, 1);
    System.out.println("Sub sequence: "+cs);
  }
}

Output:

Java StringBuffer subSequence() Output3

Recommended Posts

  • Java StringBuffer setCharAt()
  • Java StringBuffer codePointAt()
  • Java StringBuffer offsetByCodePoints()
  • Java StringBuffer charAt()
❮ Java StringBuffer

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap