Java.lang.StringBuffer.delete() Method



The java.lang.StringBuffer.delete() 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.


Following is the declaration for java.lang.StringBuffer.delete() method

public StringBuffer delete(int start, int end)


  • start − This is the beginning index, inclusive.

  • end − This is the ending index, exclusive.

Return Value

This method returns this object.


StringIndexOutOfBoundsException − if start is negative, greater than length(), or greater than end.


The following example shows the usage of java.lang.StringBuffer.delete() method.

package com.tutorialspoint;

import java.lang.*;

public class StringBufferDemo {

   public static void main(String[] args) {

      StringBuffer buff = new StringBuffer("Java lang package");
      System.out.println("buffer = " + buff);
      // deleting characters from index 4 to index 9
      buff.delete(4, 9);
      System.out.println("After deletion = " + buff);

Let us compile and run the above program, this will produce the following result −

buffer = Java lang package
After deletion = Java package