Java StringBuilder.deleteCharAt() – Examples

In this tutorial, we will learn about the Java StringBuilder.deleteCharAt() function, and learn how to use this function to remove/delete a character from StringBuilder sequence at specific index, with the help of examples.

deleteCharAt(int index)

StringBuilder.deleteCharAt() removes the char at the specified position in this StringBuilder sequence.

The index has to be within the bounds of the StringBuilder sequence.

ADVERTISEMENT

Syntax

The syntax of deleteCharAt() function is

deleteCharAt(int index)

where

ParameterDescription
indexIndex of the char to remove from the StringBuilder sequence.

Returns

The function returns reference to this StringBuilder object.

Example 1 – deleteCharAt(index)

In this example, we will initialize a StringBuilder sequence with a string literal, and initialize an index with 2 at which we would like to delete the character. We than call the deleteCharAt() on the StringBuilder with index passed as argument.

Java Program

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

Output

Before delete : abcdefghijklmn
After delete  : abdefghijklmn

Example 2 – deleteCharAt(index) in Loop

In this example, we will use deleteCharAt() in a for loop and delete all the characters from a specific index.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijklmn");
        System.out.println("Before delete : " + stringBuilder.toString());
        
        int index = 4;
        while(stringBuilder.length() > index) {
        	stringBuilder.deleteCharAt(index);
        }
        System.out.println("After delete  : " + stringBuilder.toString());
    }
}

Output

Before delete : abcdefghijklmn
After delete  : abcd

Example 3 – deleteCharAt(index) – index < 0

In this example, we will pass an index of less than zero to deleteCharAt(). Since given index is out of bounds for the StringBuilder sequence, the method should 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 index = -2;
        stringBuilder.deleteCharAt(index);
        System.out.println("After delete  : " + stringBuilder.toString());
    }
}

Output

Before delete : abcdefghijklmn
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: index -2,length 14
	at java.base/java.lang.String.checkIndex(Unknown Source)
	at java.base/java.lang.AbstractStringBuilder.deleteCharAt(Unknown Source)
	at java.base/java.lang.StringBuilder.deleteCharAt(Unknown Source)
	at Example.main(Example.java:7)

Conclusion

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