Remove a character from a Java StringBuffer object


In order to remove a character from a Java StringBuffer object, we use the deleteCharAt() method. The deleteCharAt() removes the character at the specified index. The length of resultant sequence of characters of the StringBuffer object is reduced by one.

Declaration − The java.lang.StringBuffer.deleteCharAt() method is declared as follows−

public StringBuffer deleteCharAt(int index)

Let us see a program to illustrate the use of the deleteCharAt()

Example

 Live Demo

public class Example {
   public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("Hello World");
      sb.deleteCharAt(7);
      System.out.println(sb);
   }
}

Output

Hello Wrld

Updated on: 26-Jun-2020

278 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements