Java StringBuilder delete() Method Example

Java StringBuilder delete(int start, int end) method removes the characters in a substring of this sequence. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. If start is equal to end, no changes are made.

Java StringBuilder delete() Method Example

Example to delete substring 'java' from string 'javaguides' using delete() method.
public class DeleteExample {
    public static void main(String[] args) {
        StringBuilder builder = new StringBuilder("javaguides");
  
        // start with index and end with end -1
        StringBuilder subBuilder = buffer.delete(0, 4);
         System.out.println("Delete string 'java' from string 'javaguides' : " + subBuilder.toString());
    }
}
Output:
Delete string 'java' from string 'javaguides' : guides

Reference


Comments