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 integer type which refers to the index of the char to be removed.
Return Value: The function returns the string or returns this object after removing the character.
Exception : If the indexpoint is negative or greater than or equal to length(), then the method throws StringIndexOutOfBoundsException.
Examples:
Input : StringBuffer = worldofgeeks
int indexpoint = 4
Output : worlofgeeks
Below programs illustrate the working of StringBuffer.deleteCharAt() method:
Program 1:
// Java program to demonstrate working // of StringBuffer.deleteCharAt() method import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("raghav"); System.out.println("String buffer before deletion = " + sbf); // Deleting the character at indexpoint 5 sbf.deleteCharAt(5); System.out.println("After deletion new StringBuffer = " + sbf); } } |
Output:
String buffer before deletion = raghav After deletion new StringBuffer = ragha
Program 2:
// Java program to demonstrate working // of StringBuffer.deleteCharAt() method import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("GeeksforGeeks"); System.out.println("String buffer before deletion = " + sbf); // Deleting the character at indexpoint 5 sbf.deleteCharAt(5); System.out.println("After deletion new StringBuffer = " + sbf); } } |
Output:
String buffer before deletion = GeeksforGeeks After deletion new StringBuffer = GeeksorGeeks
Program 3:
// Java program to demonstrate working // of StringBuffer.deleteCharAt() method import java.lang.*; public class Geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("Abhishek"); System.out.println("String buffer before deletion = " + sbf); // Deleting the character at indexpoint -5 sbf.deleteCharAt(-5); System.out.println("After deletion new StringBuffer = " + sbf); } } |
Output:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: -5
at java.lang.AbstractStringBuilder.deleteCharAt
(AbstractStringBuilder.java:824)
at java.lang.StringBuffer.deleteCharAt(StringBuffer.java:441)
at Geeks.main(Geeks.java:14)
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:
- StringBuilder deleteCharAt() in Java with Examples
- StringBuffer appendCodePoint() Method in Java with Examples
- StringBuffer delete() Method in Java with Examples
- StringBuffer replace() Method in Java with Examples
- StringBuffer reverse() Method in Java with Examples
- StringBuffer append() Method in Java with Examples
- StringBuffer codePointCount() method in Java with Examples
- StringBuffer codePointBefore() method in Java with Examples
- StringBuffer trimToSize() method in Java with Examples
- StringBuffer toString() method in Java with Examples
- StringBuffer codePointAt() method in Java with Examples
- StringBuffer ensureCapacity() method in Java with Examples
- StringBuffer offsetByCodePoints() method in Java with Examples
- StringBuffer setCharAt() method in Java with Examples
- StringBuffer getChars() method in Java with Examples
- StringBuffer indexOf() method in Java with Examples
- StringBuffer substring() method in Java with Examples
- StringBuffer lastIndexOf() method in Java with Examples
- Matcher appendTail(StringBuffer) method in Java with Examples
- Matcher appendReplacement(StringBuffer, String) 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.

