Java StringBuffer delete() Method



The Java StringBuffer delete() method is used to remove the elements of a substring from a sequence. The method starts removing the substring from the beginning index up to an element before the last index; that is, the start index is inclusive while the ending index is exclusive.

However, if the start index is less than end index or greater than the length of a sequence created, the method throws a StringIndexOutOfBounds Exception.

Note − If the ending index is greater than the length of the sequence, the method removes all the characters from the start index to the end of this sequence.

Syntax

Following is the syntax for the Java StringBuffer delete() method.

public StringBuffer delete(int start, int end)

Parameters

  • start − This is the beginning index, inclusive.
  • end − This is the ending index, exclusive.

Return Value

This method returns the substring of this sequnece.

Example

If the startIndex value is greater than 0 and the endIndex value is less than the sequence length, it returns the substring of this sequence.

In the following program, first, we are instantiating the StringBuffer Class with the value “Welcome to Tutorials Point”. Using the delete() method, we are trying to delete the string at a given range (startIndex = 5 and endIndex = 10.).

import java.lang.*;
public class StringBufferDemo {
   public static void main(String[] args) {
      //create a StringBuffer
      StringBuffer sb = new StringBuffer("Welcome to Tutorials Point");
      System.out.println("Before deletion the string is: " + sb);
      //initialize the startInde and endIndex values
      int startIndex = 11;
      int endIndex = 21;
      System.out.println("The startIndex and endIndex values are: " + startIndex + " and " + endIndex);
      //using the delete() method
      StringBuffer new_str = sb.delete(startIndex, endIndex);
      System.out.println("After deletion the remaing string is: " + new_str);
   }
}

Output

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

Before deletion the string is: Welcome to Tutorials Point
The startIndex and endIndex values are: 11 and 21
After deletion the remaing string is: Welcome to Point

Example

If the startIndex value is greater than the endIndex value, this method throws a StringIndexOutOfBoundsException.

In the following example, we are creating an object of the StringBuffer with the value “JavaProgramming”. Then using the delete() method, we are trying to delete the string at the given range ( where startIndex > endIndex).

public class StringBufferDemo {
   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 startIndex and endIndex values
         int startIndex = 10;// greater than the endIndex value
         int endIndex = 5;
         System.out.println("The startIndex and endIndex values are: " + startIndex + " and " + endIndex);
         //using the delete() method
         System.out.println("After deletion rhe remaing string is: " + sb.delete(startIndex, endIndex));
      } 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 startIndex and endIndex values are: 10 and 5
java.lang.StringIndexOutOfBoundsException: Range [10, 5) 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.outOfBoundsCheckFromToIndex(Preconditions.java:112)
   at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
   at java.base/java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:904)
   at java.base/java.lang.StringBuffer.delete(StringBuffer.java:475)
   at StringBufferDemo.main(StringBufferDemo.java:12)
Exception: java.lang.StringIndexOutOfBoundsException: Range [10, 5) out of bounds for length 15

Example

If the startIndex contains a negative value, the delete() method throws an IndexOutOfBoundsException.

In this example, we are creating a StringBuffer with the value“TutorialsPoint”. We are then trying to delete the string at the given range(where the startIndex holds the negative value -2) using the delete() method.

import java.lang.*;
public class StringBufferDemo {
   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 startIndex and endIndex values
         int startIndex = -2;// holds negative value
         int endIndex = 5;
         System.out.println("The startIndex and endIndex values are: " + startIndex + " and " + endIndex);
         //using the delete() method
         System.out.println("After deletion rhe remaing string is: " + sb.delete(startIndex, endIndex));
      } 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 startIndex and endIndex values are: -2 and 5
java.lang.StringIndexOutOfBoundsException: Range [-2, 5) 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.outOfBoundsCheckFromToIndex(Preconditions.java:112)
   at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
   at java.base/java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:904)
   at java.base/java.lang.StringBuffer.delete(StringBuffer.java:475)
   at StringBufferDemo.main(StringBufferDemo.java:14)
Exception: java.lang.StringIndexOutOfBoundsException: Range [-2, 5) out of bounds for length 14
java_lang_stringbuffer.htm
Advertisements