Home » Java programming language

Java StringBuffer CharSequence subSequence(int spos, int epos) method with Example

Java StringBuffer CharSequence subSequence(int spos, int epos) method: Here, we are going to learn about the CharSequence subSequence(int spos, int epos) method of StringBuffer class with its syntax and example.
Submitted by Preeti Jain, on July 03, 2019

StringBuffer Class CharSequence subSequence(int spos, int epos)

  • This method is available in package java.lang.StringBuffer.subSequence(int spos , int epos ).
  • This method is used to return a character sequence that is a substring in the given StringBuffer object and substring starts from spos(Starting position) and ends with epos(Ending position).
  • The starting position (spos) will be the part of substring i.e. it is included in the returning substring and ending position(epos) will not be the part of substring i.e. it is excluded in the returning substring.


    CharSequence subSequence(int spos , int epos){


We pass only two objects in the method of the StringBuffer i.e. spos(starting position), epos(ending position). The returning substring is in between spos and epos.

Return value:

The return type of this method is CharSequence that means this method returns the character sequence of the given StringBuffer object.

Java program to demonstrate example of subSequence() method

import java.lang.StringBuffer;

public class StringBufferClass {
    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer(" java.util is a package of Java ");

        // use subSequence(int spos , int epos) 
        // it will retrieve character sequence between 
        // spos and epos with the given string in the method.

        // Display result after implementing subSequence(0,23) 
        // i.e. "java.util is a package" will be returned 
        // by the StringBuffer object "java.util is a package of Java"
        System.out.println("The result will be after implementing method is :" + sb.subSequence(0, 23));



D:\Programs>java StringBufferClass
The result will be after implementing method is : java.util is a package


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.