Javatpoint Logo
Javatpoint Logo

Java StringBuilder delete() method

The delete(int start, int end) method of Java StringBuilder class is used to delete the substring from specified start index to exclusive end index of this sequence. If the start and end index were same then no deletion takes place and this sequence remains unchanged.

Syntax:

Parameter:

DataType Parameter Description
StringBuilder start The start index of character sequence.
StringBuilder end The exclusive end index of a character sequence.

Returns:

The delete(int start, int end) method returns this object.

Exception:

StringIndexOutOfBoundsException - if start index is negative or any of index are greater than the length(), or the start index is greater than the end index.

Compatibility Version:

Java 1.5 and above

Example 1

Test it Now

Output:

string1 = javatpoint
After deleting = jaoint
string2 = let us learn java
After deleting = learn java

Example 2

Providing the negative (or less than 0) index in delete(int,int) method throws an exception.

Test it Now

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -2
	at java.lang.AbstractStringBuilder.delete(Unknown Source)
	at java.lang.StringBuilder.delete(Unknown Source)
	at snippet.StringBuilderDeleteExample2.main(StringBuilderDeleteExample2.java:6)
string = javatpoint

Example 3

Providing the start index and end index as same value, no deletion take place and this sequence remains unchanged.

Test it Now

Output:

string = java code
After deleting = java code

Example 4

Taking the string input from a user in string builder and using in delete(int,int) method.

Test it Now

Output:

enter your string value: string builder
enter start index: 3
enter end index: 7
Count = strbuilder






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA