Home » Java programming language

Java StringBuffer deleteCharAt(int index) method with Example

Java StringBuffer deleteCharAt(int index) method: Here, we are going to learn about the deleteCharAt(int index) method of StringBuffer class with its syntax and example.
Submitted by Preeti Jain, on June 30, 2019

StringBuffer Class deleteCharAt(int index)

  • This method is available in package java.lang.StringBuffer.deleteCharAt(int index).
  • This method is used to delete the character at the given index in the method.
  • This method raises an exception StringIndexOutOfBoundsException if the given index is negative or greater than the length.

Syntax:

    StringBuffer deleteCharAt(int index){
    }

Parameter(s):

We can pass only one object in the method of the StringBuffer i.e. index.

Return value:

The return type of this method is StringBuffer that means this method returns a reference of this object.

Java program to demonstrate example of deleteCharAt() method

import java.lang.StringBuffer;

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

        StringBuffer sb = new StringBuffer(" welcome in java world ");

        // use deleteCharAt(int index) it will delete the 
        // character at the given index.

        // Display result after implementing deleteCharAt(15)

        System.out.println("The result will be after implementing method deleteCharAt(15) is :" + sb.deleteCharAt(15));

        sb = new StringBuffer("Version is : 8");

        // use deleteCharAt(int index) it will delete the character 
        // at the given index

        // Display result after implementing deleteCharAt(12)
        System.out.println("The result will be after implementing method deleteCharAt(12) is :" + sb.deleteCharAt(12));
    }
}

Output

D:\Programs>javac StringBufferClass.java

D:\Programs>java StringBufferClass
The result will be after implementing method deleteCharAt(15) is :welcome in javaworld
The result will be after implementing method deleteCharAt(12) is :Version is :8	


Comments and Discussions!

Load comments ↻





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