StringBuilder delete() in Java with Examples

The delete(int start, int end) method of StringBuilder class removes the characters starting from index start to index end-1 from String contained by StringBuilder. This method takes two indexs as a parameter first start represents index of the first character and endIndex represents index after the last character of the substring to be removed from String contained by StringBuilder and returns the remaining String as StringBuilder Object.

Syntax:

public StringBuilder delete(int start, int end)

Parameters: This method accepts two parameters:

  • start: index of the first character of the substring.
  • end: index after the last character of the substring.

Return Value: This method returns this StringBuilder object after removing the substring.

Exception: This method throws StringIndexOutOfBoundsException if the start is less than zero, or start is larger than the length of String, or start is larger than end.

Below programs demonstrate the delete() method of StringBuilder Class:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the delete() Method.
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder
            str
            = new StringBuilder("WelcomeGeeks");
  
        // print string
        System.out.println("Before removal String = "
                           + str.toString());
  
        // remove the substring from index 2 to 8
        StringBuilder afterRemoval = str.delete(2, 8);
  
        // print string after removal of substring
        System.out.println("After removal String = "
                           + afterRemoval.toString());
    }
}

chevron_right


Output:

Before removal String = WelcomeGeeks
After removal String = Weeeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the delete() Method.
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder
            str
            = new StringBuilder("GeeksforGeeks");
  
        // print string
        System.out.println("Before removal String = "
                           + str.toString());
  
        // remove the substring from index 8 to 8
        StringBuilder afterRemoval = str.delete(8, 8);
  
        // start equal to end so no change in string
        // print string after removal of substring
        System.out.println("After removal of SubString"
                           + " start=8 to end=8"
                           + " String becomes => "
                           + afterRemoval.toString());
  
        // remove the substring from index 1 to 8
        afterRemoval = str.delete(1, 8);
  
        // print string after removal of substring
        System.out.println("After removal of SubString"
                           + " start=1 to end=8"
                           + " String becomes => "
                           + afterRemoval.toString());
    }
}

chevron_right


Output:

Before removal String = GeeksforGeeks
After removal of SubString start=8 to end=8 String becomes => GeeksforGeeks
After removal of SubString start=1 to end=8 String becomes => GGeeks

Example 3: To demonstrate IndexOutOfBoundException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// exception thrown by the delete() Method.
  
class GFG {
    public static void main(String[] args)
    {
  
        // create a StringBuilder object
        // with a String pass as parameter
        StringBuilder
            str
            = new StringBuilder("GeeksforGeeks");
  
        try {
  
            // make start greater than end
            StringBuilder afterRemoval = str.delete(7, 4);
        }
  
        catch (Exception e) {
            System.out.println("Exception: " + e);
        }
    }
}

chevron_right


Output:

Exception: java.lang.StringIndexOutOfBoundsException

Reference:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#delete(int, int)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.