Remove a character from a Java StringBuffer object

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 03-Jan-2019 11:47:56
Advertisements