The Wayback Machine - https://web.archive.org/web/20240726024333/https://www.geeksforgeeks.org/stringbuffer-deletecharat-method-in-java/
Open In App

StringBuffer deleteCharAt() Method in Java with Examples

Last Updated : 06 Dec, 2018
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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)


Similar Reads

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 StringBuilder is shortened by one char after application
3 min read
StringBuffer appendCodePoint() Method in Java with Examples
appendCodePoint() method of StringBuffer class appends the string representation of the codePoint argument to this sequence for which we require pre-requisite knowledge of ASCII table as then only we will be able to perceive output why the specific literal is being appended as there is already an integer value been assigned. --> appendCodePoint(
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_point, int end_point) Parameters : The method acce
3 min read
StringBuffer replace() Method in Java with Examples
The StringBuffer.replace() is the inbuilt method which is used to replace the characters in a substring of this sequence with the characters in the specified String. Here simply the characters in the substring are removed and other char is inserted at the start.Syntax : public StringBuffer replace(int first, int last, String st) Parameters : The me
2 min read
StringBuffer reverse() Method in Java with Examples
The Java.lang.StringBuffer.reverse() is an inbuilt method that is used to reverse the characters in the StringBuffer. The method causes this character sequence to be replaced by the reverse of the sequence. Syntax: public StringBuffer reverse() Parameters: NA Return Value: The method returns the StringBuffer after reversing the characters. Examples
2 min read
StringBuffer append() Method in Java with Examples
Pre-requisite: StringBuffer class in Java The java.lang.StringBuffer.append() method is used to append the string representation of some argument to the sequence. There are 13 ways/forms in which the append() method can be used: StringBuffer append(boolean a) :The java.lang.StringBuffer.append(boolean a) is an inbuilt method in Java which is used t
13 min read
StringBuffer codePointCount() method in Java with Examples
The codePointCount() method of StringBuffer class is used to return the number of Unicode code points in the specified range of beginIndex to endIndex of String contained by StringBuffer. This method takes beginIndex and endIndex as a parameter where beginIndex is the index of the first character of the text range and endIndex is index after the la
3 min read
StringBuffer codePointBefore() method in Java with Examples
The codePointBefore() method of StringBuffer class is a method used to take an index as a parameter and returns the “Unicode number” of the character present before that index. The value of index must lie between 0 to length-1. If the char value at (index – 1) is in the low-surrogate range, char at (index – 2) is not negative with value is in the h
2 min read
StringBuffer trimToSize() method in Java with Examples
The trimToSize() method of StringBuffer class is the inbuilt method used to trims the capacity used for the character sequence of StringBuffer object. If the buffer used by StringBuffer object is larger than necessary to hold its current sequence of characters, then this method is called to resize the StringBuffer object for converting this object
2 min read
StringBuffer toString() method in Java with Examples
The toString() method of StringBuffer class is the inbuilt method used to returns a string representing the data contained by StringBuffer Object. A new String object is created and initialized to get the character sequence from this StringBuffer object and then String is returned by toString(). Subsequent changes to this sequence contained by Obje
2 min read
three90RightbarBannerImg