The Wayback Machine - https://web.archive.org/web/20240726035856/https://www.geeksforgeeks.org/stringbuilder-delete-in-java-with-examples/
Open In App

StringBuilder delete() in Java with Examples

Last Updated : 22 Sep, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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 indexes 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:  

Java




// 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());
    }
}


Output

Before removal String = WelcomeGeeks
After removal String = Weeeks

Example 2: 

Java




// 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());
    }
}


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

Java




// 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);
        }
    }
}


Output: 

Exception: java.lang.StringIndexOutOfBoundsException

 

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



Similar Reads

StringBuilder Class in Java with Examples
StringBuilder in Java represents a mutable sequence of characters. Since the String Class in Java creates an immutable sequence of characters, the StringBuilder class provides an alternative to String Class, as it creates a mutable sequence of characters. The function of StringBuilder is very much similar to the StringBuffer class, as both of them
6 min read
StringBuilder charAt() in Java with Examples
The charAt(int index) method of StringBuilder Class is used to return the character at the specified index of String contained by StringBuilder Object. The index value should lie between 0 and length()-1. Syntax: public char charAt(int index) Parameters: This method accepts one int type parameter index which represents index of the character to be
3 min read
StringBuilder codePointAt() in Java with Examples
The codePointAt(int index) method of StringBuilder class takes an index as a parameter and returns a character unicode point at that index in String contained by StringBuilder or we can say charPointAt() method returns the "unicode number" of the character at that index. The index refers to char values (Unicode code units) and the value of index mu
4 min read
StringBuilder append() Method in Java With Examples
The java.lang.StringBuilder.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used by the passing of various types of arguments: StringBuilder append(boolean a) :The java.lang.StringBuilder.append(boolean a) is an inbuilt method in Java which is
13 min read
StringBuilder codePointCount() in Java with Examples
The codePointCount() method of StringBuilder class returns the number of Unicode code points in the specified text range in String contained by StringBuilder. This method takes two indexes as a parameter- first beginIndex which represents index of the first character of the text range and endIndex which represents index after the last character of
3 min read
StringBuilder capacity() in Java with Examples
The capacity() method of StringBuilder Class is used to return the current capacity of StringBUilder object. The capacity is the amount of storage available to insert new characters.Syntax: public int capacity() Return Value: This method returns the current capacity of StringBuilder Class.Below programs demonstrate the capacity() method of StringBu
2 min read
StringBuilder codePointBefore() in Java with Examples
The codePointBefore() method of StringBuilder class takes an index as a parameter and returns the "Unicode number" of the character before the specified index in String contained by StringBuilder. The index refers to char values (Unicode code units) and the value of index must lie between 0 to length-1. If the char value at (index - 1) is in the lo
2 min read
StringBuilder deleteCharAt() in Java with Examples
The deleteCharAt(int index) method of StringBuilder class remove the character at the given index from String contained by StringBuilder. This method takes index as a parameter which represents the index of char we want to remove and returns the remaining String as StringBuilder Object. This StringBuilder is shortened by one char after application
3 min read
StringBuilder ensureCapacity() in Java with Examples
The ensureCapacity(int minimumCapacity) method of StringBuilder class helps us to ensures the capacity is at least equal to the specified minimumCapacity passed as the parameter to the method. If the current capacity of StringBuilder is less than the argument minimumCapacity, then a new internal array is allocated with greater capacity. If the mini
2 min read
StringBuilder getChars() in Java with Examples
The getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method of StringBuilder class copies the characters starting at the given index:srcBegin to index:srcEnd-1 from String contained by StringBuilder into an array of char passed as parameter to function. The characters are copied from StringBuilder into the array dst[] starting at index:
3 min read
Practice Tags :
three90RightbarBannerImg