Java StringBuilder.delete() – Examples

In this tutorial, we will learn about the Java StringBuilder.delete() function, and learn how to use this function to remove characters in specific index range, with the help of examples.

delete(int start, int end)

StringBuilder.delete() removes the characters in a substring of this sequence and returns reference to this StringBuilder object.

Syntax

The syntax of delete() function is

delete(int start, int end)

where

ParameterDescription
startThe beginning index, of the substring to delete. This position is inclusive for delete operation.
endThe ending index, of the substring to delete. This position is exclusive for delete operation.

Returns

The function returns the reference to this StringBuilder object.

Example 1 – delete(start, end)

In this example, we will initialize a StringBuilder object with a string literal, delete a substring that starts at index 4 and spans until index 8.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
        System.out.println("Before delete : " + stringBuilder.toString());
        
        int start = 4;
        int end = 8;
        stringBuilder.delete(start, end);
        System.out.println("After delete  : " + stringBuilder.toString());
    }
}

Output

Before delete : abcdefghijklmn
After delete  : abcdijklmn

The substring efgh is deleted from the sequence.

Example 2 – delete(start, end) – start > end

In this example, we will give the value for start such that it is greater than end. This should make delete() throw java.lang.StringIndexOutOfBoundsException.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
        System.out.println("Before delete : " + stringBuilder.toString());
        
        int start = 8;
        int end = 5;
        stringBuilder.delete(start, end);
        System.out.println("After delete  : " + stringBuilder.toString());
    }
}

Output

Before delete : abcdefghijklmn
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start 8, end 5, length 14
	at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(Unknown Source)
	at java.base/java.lang.AbstractStringBuilder.delete(Unknown Source)
	at java.base/java.lang.StringBuilder.delete(Unknown Source)
	at Example.main(Example.java:8)

Example 3 – delete(start, end) – end > length

In this example, we will give the value of end greater than length of sequence. Since end > sequence length, delete() will delete the substring that begins at start index till the end of sequence.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
        System.out.println("Before delete : " + stringBuilder.toString());
        
        int start = 5;
        int end = 75;
        stringBuilder.delete(start, end);
        System.out.println("After delete  : " + stringBuilder.toString());
    }
}

Output

Before delete : abcdefghijklmn
After delete  : abcde

Example 4 – delete(start, end) – start < 0

In this example, we will give the value of start less than zero. This should make delete() throw java.lang.StringIndexOutOfBoundsException.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
        System.out.println("Before delete : " + stringBuilder.toString());
        
        int start = -5;
        int end = 5;
        stringBuilder.delete(start, end);
        System.out.println("After delete  : " + stringBuilder.toString());
    }
}

Output

Before delete : abcdefghijklmn
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start -5, end 5, length 14
	at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(Unknown Source)
	at java.base/java.lang.AbstractStringBuilder.delete(Unknown Source)
	at java.base/java.lang.StringBuilder.delete(Unknown Source)
	at Example.main(Example.java:8)

Conclusion

In this Java Tutorial, we have learnt the syntax of Java StringBuilder.delete() function, and also learnt how to use this function with the help of examples.