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.
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:
Before removal String = WelcomeGeeks After removal of character at index 8 = WelcomeGeks
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
Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: 14
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.