Remove substring from StringBuilder in Java

Java 8Object Oriented ProgrammingProgramming

In order to remove a substring from a Java StringBuilder 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.StringBuilder.delete() method is declared as follows −

public StringBuilder delete(int start,int end)

Let us see an example which removes a substring from a StringBuilder.

Example

 Live Demo

public class Example {
   public static void main(String[] args) {
      StringBuilder sb = new StringBuilder("Welcome to Java Programming");
      System.out.println("Original StringBuilder Object: " + sb.toString());
      sb.delete(4,10);
      System.out.println("New StringBuilder Object: " + sb.toString());
   }
}

Output

Original StringBuffer Object: Welcome to Java Programming
New StringBuffer Object: Welc Java Programming
raja
Published on 03-Jan-2019 11:55:41

Advertisements