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 of this method.
Syntax:
public StringBuilder deleteCharAt(int index)
Parameters: This method accepts one parameter index represents index of the character you want to remove.
Return Value: This method returns this StringBuilder object after removing the character.
Exception: This method throws StringIndexOutOfBoundsException if the index is less than zero, or index is larger than the length of String.
Below programs demonstrate the deleteCharAt() method of StringBuilder Class:
Example 1:
// Java program to demonstrate // the deleteCharAt() 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 Character from index 8 StringBuilder afterRemoval = str.deleteCharAt(8); // print string after removal of Character System.out.println("After removal of character" + " at index 8 = " + afterRemoval.toString()); } } |
Before removal String = WelcomeGeeks After removal of character at index 8 = WelcomeGeks
Example 2:
// Java program to demonstrate // the deleteCharAt() 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 Character from index 3 str = str.deleteCharAt(3); // print string after removal of Character System.out.println("After removal of Character" + " from index=3" + " String becomes => " + str.toString()); // remove the substring from index 5 str = str.deleteCharAt(5); // print string after removal of Character System.out.println("After removal of Character" + " from index=5" + " String becomes => " + str.toString()); } } |
Before removal String = GeeksforGeeks After removal of Character from index=3 String becomes => GeesforGeeks After removal of Character from index=5 String becomes => GeesfrGeeks
Example 3: To demonstreate IndexOutOfBoundException
// Java program to demonstrate // exception thrown by the deleteCharAt() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("evil dead_01"); try { // make index > length of String StringBuilder afterRemoval = str.deleteCharAt(14); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 14
Reference:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#deleteCharAt(int)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- StringBuffer deleteCharAt() Method in Java with Examples
- StringBuilder Class in Java with Examples
- StringBuilder charAt() in Java with Examples
- StringBuilder codePointAt() in Java with Examples
- StringBuilder append() Method in Java With Examples
- StringBuilder delete() in Java with Examples
- StringBuilder codePointCount() in Java with Examples
- StringBuilder capacity() in Java with Examples
- StringBuilder codePointBefore() in Java with Examples
- StringBuilder ensureCapacity() in Java with Examples
- StringBuilder getChars() in Java with Examples
- StringBuilder length() in Java with Examples
- StringBuilder offsetByCodePoints() method in Java with Examples
- StringBuilder replace() in Java with Examples
- StringBuilder setCharAt() in Java with Examples
- StringBuilder reverse() in Java with Examples
- StringBuilder setLength() in Java with Examples
- StringBuilder subSequence() in Java with Examples
- StringBuilder substring() method in Java with examples
- StringBuilder toString() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

