Java StringBuffer deleteCharAt() Method Last Updated : 10 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, the deleteCharAt() method of the StringBuffer class is used to delete the character at a specified index in the string. Example 1: The below Java program demonstrates the use of deleteCharAt() to remove a character at a specific index from the string. Java // Removing a character at a specific index class Geeks { public static void main(String[] args) { StringBuffer s = new StringBuffer("Hello, World!"); System.out.println("Original String: " + s); // Delete character at index 5 s.deleteCharAt(5); System.out.println("New String: " + s); } } OutputOriginal String: Hello, World! New String: Hello World! Note: After deletion, the subsequent characters are shifted left. It’s important to handle index bounds properly to avoid the StringIndexOutOfBoundsException.SyntaxStringBuffer deleteCharAt(int index)Parameter: index - The element you want to delete.Return Type: This method returns the modified StringBuffer object with the character removed.Note: If the index value is negative or an index greater than or equal to the length of the sequence, this methods throws an StringIndexOutOfBoundsExceptionExample 2: The below Java program demonstrates deleting a character at an invalid index throws a StringIndexOutOfBoundsException and handle it with a try-catch block. Java // Handling invalid index with exception public class Geeks { public static void main(String[] args) { try { // Create an object of the StringBuffer StringBuffer sb = new StringBuffer("HelloWorld"); System.out.println( "Before deletion, the string is: " + sb); // Initialize the index value int i = -5; // Invalid negative index System.out.println("The given index value is: " + i); // Using the deleteCharAt() method System.out.println( "After deletion, the remaining string is: " + sb.deleteCharAt(i)); } catch (IndexOutOfBoundsException e) { e.printStackTrace(); System.out.println("Exception: " + e); } } } Output: Comment More infoAdvertise with us Next Article Java StringBuilder delete(int start, int end) Method J juhisrivastav Follow Improve Article Tags : Java java-StringBuffer Practice Tags : Java Similar Reads Java StringBuffer capacity() Method In Java, the capacity() method is used in the StringBuilder and StringBuffer classes to retrieve the current capacity of the object. The capacity is the amount of space that has been allocated to store the string that can grow dynamically as needed.Example 1: The below Java program demonstrates the 3 min read StringBuffer deleteCharAt() Method in Java with Examples The Java.lang.StringBuffer.deleteCharAt() is a built-in Java method which removes the char at the specified position in this sequence. So that the sequence is reduced by 1 char. Syntax: public StringBuffer deleteCharAt(int indexpoint) Parameters : The method accepts a single parameter indexpoint of 2 min read StringBuffer getChars() method in Java with Examples The getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) method of StringBuffer class copies the characters starting at the index:srcBegin to index:srcEnd-1 from actual sequence into an array of char passed as parameter to function. The characters are copied into the array of char dst[] star 3 min read StringBuffer delete() Method in Java with Examples The java.lang.StringBuffer.delete() is an inbuilt method in Java which is used to remove or delete the characters in a substring of this sequence. The substring starts at a specified index start_point and extends to the character at the index end_point. Syntax : public StringBuffer delete(int start_ 3 min read Java StringBuilder delete(int start, int end) Method delete(int start, int end) method in the StringBuilder class is used to remove a portion of the string, starting from the specified start index to the specified end index. This method is used to modify mutable sequences of characters.Example 1: The below example demonstrates how to use the delete() 2 min read StringBuilder deleteCharAt() in Java with Examples 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 StringB 3 min read Like