Java: StringBuilder delete() method

In this article, we will discuss StringBuilder’s delete() method which deletes sub-string starting from specified start-index to end-index


delete() method:

  • This method deletes portion of the invoking StringBuilder
  • That’s sub-string starting from specified start index-position till end-1 index-position

Method Signature:

public StringBuilder delete(int start, int end);


  • Returns resulting StringBuilder object after deleting portion of string or sub-string
  • Sub-string starts from specified start index-position and ends at specified end index-position
  • Note: Start index-position is inclusive and end index-position exclusive


StringIndexOutOfBoundsException, if index value passed falls out of range i.e.;

  • if either start-index or end-index is negative (<0)
  • if start-index is greater than end-index
  • if end-index is greater than length()


Examples on delete() method:

  • To delete sub-string from invoking StringBuilder object for the specified range

package in.bench.resources.string.methods;

public class StringBuilderDeleteMethod {

	public static void main(String[] args) {

		// StringBuilder - 1
		StringBuilder sb1 = new StringBuilder("");

		// removes character from start-index to end-index
		sb1.delete(3, 6); 

		// print to console
		System.out.println("1. removing "
				+ "StringBuilder content from 3-6 : " + sb1);

		// StringBuilder - 2
		StringBuilder sb2 = new StringBuilder(""); 

		// to clear all contents of StringBuilder
		sb2.delete(0, sb2.length()); 

		// print to console
		System.out.println("2. after clearing "
				+ "StringBuilder contents   : " + sb2);


1. removing StringBuilder content from 3-6 :
2. after clearing StringBuilder contents   : 




Happy Coding !!
Happy Learning !!

Java: StringBuilder deleteCharAt() method
Java: StringBuilder charAt() method