Java StringBuilder delete() Method



The Java StringBuilder delete() method is used to remove the characters in a substring of a sequence. The substring is the smaller part of the given string.

The substring begins at the specified startIndex and extends to the character at index endIndex - 1 or, the endIndex of the sequence if no such character exists. If the startIndex is equal to the endIndex, no changes are made.

The delete() method accepts two parameters as an integer that holds the value of startIndex and endIndex. It throws an exception if the startIndex value is negative, the endIndex value is greater than the sequence length, and the startIndex is greater than the end index.

Syntax

Following is the syntax of the Java StringBuilder delete() method −

public StringBuilder 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 StringBuilder 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.).

package com.tutorialspoint.StringBuilder;
import java.lang.*;
public class StringBuilderDemo {
   public static void main(String[] args) {
      
      //create s StringBuilder
      StringBuilder sb = new StringBuilder("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
      StringBuilder 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 StringBuilder with the value “JavaProgramming”, Using the delete() method, we are trying to delete the string at the given range ( where startIndex > endIndex).

package com.tutorialspoint.StringBuilder;
public class Demo {
   public static void main(String[] args) {
      try {
         
         //create an object of the StringBuilder
         StringBuilder sb = new StringBuilder("TutorialsPoint");
         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: TutorialsPoint
The startIndex and endIndex values are: 10 and 5
java.lang.StringIndexOutOfBoundsException: start 10, end 5, length 14
	at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(AbstractStringBuilder.java:1810)
	at java.base/java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:917)
	at java.base/java.lang.StringBuilder.delete(StringBuilder.java:289)
	at com.tutorialspoint.StringBuilder.Demo.main(Demo.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: start 10, end 5, length 14

Example

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

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

package com.tutorialspoint.StringBuilder;
import java.lang.*;
public class Delete {
   public static void main(String[] args) {
      try {
         
         //create an object of the StringBuilder
         StringBuilder sb = new StringBuilder("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: start -2, end 5, length 14
	at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(AbstractStringBuilder.java:1810)
	at java.base/java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:917)
	at java.base/java.lang.StringBuilder.delete(StringBuilder.java:289)
	at com.tutorialspoint.StringBuilder.Delete.main(Delete.java:13)
Exception: java.lang.StringIndexOutOfBoundsException: start -2, end 5, length 14
java_lang_stringbuilder.htm
Advertisements