java.lang.StringBuilder deleteCharAt(int index)


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.


  • 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.


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.


 * A java example source code to demonstrate
 * the use of deleteCharAt() method of StringBuilder class

public class StringBuilderDeleteExample {

	public static void main(String[] args) {

		// initialize the StringBuffer object
		StringBuilder sb = new StringBuilder("");
		System.out.println("Contents of buffer:" + sb);

		int index = 5;

		// delete the character at index 5
		System.out.println("New Contents of Buffer:" + sb);

The java example source code above, demonstrates the use of deleteCharAt(int index) method of StringBuilder class. Initially the code assigns a string “” 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