StringBuffer delete() and deleteCharAt()

delete() and deleteCharAt()

You can delete characters between StringBuffer using the delete() method and deleteCharAt() method. These methods are shown here :

StringBuffer delete(int start, int end)
StringBuffer deleteCharAt(int location)

The delete() method deletes the order of the characters from the driven object. Here, specify the index of the first character to delete the start and specify an index in one of the last characters to delete the end. Thus, the deleted sections run from start to end. As a result, the string buffer object returns.

The deleteCharAt() method deletes characters in specific indicators by location. It provides the StringBuffer object of the result.


public class Main 
  public static void main(String[] args) 
    StringBuffer sb = new StringBuffer("I am a good boy");
    System.out.println("Before delete = " + sb);
    System.out.println("After delete = " + sb.delete(4,6));
    System.out.println("After deleteCharAt = " + sb.deleteCharAt(5));