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.

Syntax:

    CharSequence subSequence(int spos , int epos){
    }

Parameter(s):

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));
    }
}

Output

D:\Programs>javac StringBufferClass.java

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



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.