Remove characters in a range from a Java StringBuffer Object

Java 8Object Oriented ProgrammingProgramming

In order to remove characters in a specific range from a Java StringBuffer Object, we use the delete() method. The delete() method removes characters in a range from the sequence. The delete() method has two parameters , start and end. Characters are removed from start to end-1 index.

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

Let us see a program to delete characters in a specific range from a Java StringBuffer Object.

Example

 Live Demo

public class Example {
   public static void main(String[] args) {
      StringBuffer sb = new StringBuffer("Hello World");
      System.out.println("Original StringBuffer Object: " + sb);
      sb.delete(4,8);
      System.out.println("New StringBuffer Object: " + sb);
   }
}

Output

Original StringBuffer Object: Hello World
New StringBuffer Object: Hellrld
raja
Published on 03-Jan-2019 16:21:54
Advertisements