.delete()
Published Aug 22, 2022
Contribute to Docs
The .delete()
method removes a substring from the contents of a StringBuilder
and returns the modified object.
Syntax
myStringbuilder.delete(start, end);
The int
arguments start
and end
specify the zero-based start and end character indices of the substring to delete. The last character deleted is at end
- 1. If end
is past the end of the sequence stored in the StringBuilder
, characters are deleted through the end of the sequence. If start
is equal to end
, no changes are made. If start
is greater than end
, past the end of the sequence in the StringBuilder
, or is negative, a StringIndexOutOfBoundsException
is thrown.
Example
The following example creates a StringBuilder
with a specified String
and then uses the .delete()
method to change its contents:
import java.util.*;public class Example {public static void main(String[] args){StringBuilder str = new StringBuilder("Hello World!");System.out.println(str.toString());str.delete(0,6);System.out.println(str.toString());}}
This produces the following output:
Hello World!World!
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours