java.lang.StringBuilder deleteCharAt(int index)

Description

On this document we will be showing a java example on how to use the deleteCharAt(int index) method of StringBuilder Class. Basically the deleteCharAt(int index) method removes/delete a character on the character sequence on the specified index.

Notes:

  • A StringIndexOutOfBoundsException will be thrown by the deleteCharAt() method if the index is negative or greater than or equal to length().
  • It will yield the same result as calling the delete() with the start value being equal to index and the end argument equals to index + 1.

Method Syntax

public StringBuilder deleteCharAt(int index)

Method Argument

Data Type Parameter Description
int index index of the char to be removed.

Method Returns

The deleteCharAt(int index) method returns this object with a character removed as specified by index argument.

Compatibility

Requires Java 1.5 and up

Java StringBuilder deleteCharAt(int index) Example

Below is a java code demonstrates the use of deleteCharAt(int index) method of StringBuilder class. The example presented might be simple however it shows the behavior of the deleteCharAt() method.

The java example source code above, demonstrates the use of deleteCharAt(int index) method of StringBuilder class. Initially the code assigns a string “javatutorialhq.com” as initial contents of the string buffer. Then we use deleteCharAt method to delete the character of the StringBuilder on index 5.

Sample Output

Below is the sample output when you run the above example.

StringBuilder deleteCharAt() example output