Java StringBuffer deleteCharAt() Method



The java StringBuffer deleteCharAt() method is used to remove the character at the specified position in this sequence. This sequence is shortened by one character.

In Java, the indexes are refers to the character positions of the current sequence. Basically, the charAt() method is used to retrieve a character at the specified index.The deleteCharAt() method accepts a parameter as an integer that holds the value of the character index. It throws an exception if the index value is negative or the value is greater than the given sequence length.

Syntax

Following is the syntax of the Java StringBuffer deleteCharAt() method −

public StringBuffer deleteCharAt(int index)

Parameters

  • index − This is the index of char to remove.

Return Value

This method returns the substring of the given sequence.

Example

If the index value is non-negative and less than the sequence length, this method returns the new substring of the given sequence.

In the following example,we are creating an object of the StringBuffer class with the value “Java lang package”. Using the delete() method, we are trying to delete a character at the specified index 3.

import java.lang.*;
public class StringBufferDemo {
   public static void main(String[] args) {
      //create an object of the StringBuffer
      StringBuffer sb = new StringBuffer("Java lang package");
      System.out.println("Before deletion the string is: " + sb);
      //initialize the index value
      int index = 3;
      System.out.println("The given index value is: " + index);
      //using the deleteCharAt() method
      System.out.println("After deletion the character at the specified index " + index + " index is: " + sb.deleteCharAt(index));
   }
}

Output

On executing the above program, it will produce the following result −

Before deletion the string is: Java lang package
The given index value is: 3
After deletion the character at the specified index 3 index is: Jav lang package

Example

If the index value if greater than the given sequence length, this method throws an StringIndexOutOfBoundsException.

In this program,then we are instantiating the StringBuffer class with the value “JavaProgramming”. Using the deleteCharAt() method, we are trying to delete a character at the specified index 30, whose value is greater than the given sequence.

public class Delete {
   public static void main(String[] args) {
      try {
         //create an object of the StringBuffer
         StringBuffer sb = new StringBuffer("JavaProgramming");
         System.out.println("Before deletion the string is: " + sb);
         //initialize the index value
         int index = 30;// value is greater than the sequence length
         System.out.println("The given index value is: " + index);
         //using the deleteCharAt() method
         System.out.println("After deletion rhe remaing string is: " + sb.deleteCharAt(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

Before deletion the string is: JavaProgramming
The given index value is: 30
java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 15
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
   at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
   at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
   at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
   at java.base/java.lang.String.checkIndex(String.java:4557)
   at java.base/java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:957)
   at java.base/java.lang.StringBuffer.deleteCharAt(StringBuffer.java:486)
   at Delete.main(Delete.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: Index 30 out of bounds for length 15

Example

If the index value contains a negative value, it throws a StringIndexOutOfBoundsException, when we invoke the method on it.

In the following example, we create a StringBuffer with the value “TutorialsPoint”. Using the deleterCharAt() method, we are trying to delete the character at the specified index -1.

public class Demo {
   public static void main(String[] args) {
      try {
         //create an object of the StringBuffer
         StringBuffer sb = new StringBuffer("TutorialsPoint");
         System.out.println("Before deletion the string is: " + sb);
         //initialize the index value
         int index = -1;// holds the negative value
         System.out.println("The given index value is: " + index);
         //using the deleteCharAt() method
         System.out.println("After deletion rhe remaing string is: " + sb.deleteCharAt(index));
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

The above program, produces the following results −

Before deletion the string is: TutorialsPoint
The given index value is: -1
java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 14
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
   at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
   at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
   at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
   at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
   at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
   at java.base/java.lang.String.checkIndex(String.java:4557)
   at java.base/java.lang.AbstractStringBuilder.deleteCharAt(AbstractStringBuilder.java:957)
   at java.base/java.lang.StringBuffer.deleteCharAt(StringBuffer.java:486)
   at Demo.main(Demo.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: Index -1 out of bounds for length 14
java_lang_stringbuffer.htm
Advertisements