StringBuilder delete() in Java with Examples Last Updated : 22 Sep, 2021 Comments Improve Suggest changes Like Article Like Report The delete(int start, int end) method of StringBuilder class removes the characters starting from index start to index end-1 from String contained by StringBuilder. This method takes two indexes as a parameter first start represents index of the first character and endIndex represents index after the last character of the substring to be removed from String contained by StringBuilder and returns the remaining String as StringBuilder Object. Syntax: public StringBuilder delete(int start, int end) Parameters: This method accepts two parameters: start: index of the first character of the substring.end: index after the last character of the substring. Return Value: This method returns this StringBuilder object after removing the substring. Exception: This method throws StringIndexOutOfBoundsException if the start is less than zero, or start is larger than the length of String, or start is larger than end. Below programs demonstrate the delete() method of StringBuilder Class: Example 1: Java // Java program to demonstrate // the delete() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("WelcomeGeeks"); // print string System.out.println("Before removal String = " + str.toString()); // remove the substring from index 2 to 8 StringBuilder afterRemoval = str.delete(2, 8); // print string after removal of substring System.out.println("After removal String = " + afterRemoval.toString()); } } OutputBefore removal String = WelcomeGeeks After removal String = Weeeks Example 2: Java // Java program to demonstrate // the delete() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("GeeksforGeeks"); // print string System.out.println("Before removal String = " + str.toString()); // remove the substring from index 8 to 8 StringBuilder afterRemoval = str.delete(8, 8); // start equal to end so no change in string // print string after removal of substring System.out.println("After removal of SubString" + " start=8 to end=8" + " String becomes => " + afterRemoval.toString()); // remove the substring from index 1 to 8 afterRemoval = str.delete(1, 8); // print string after removal of substring System.out.println("After removal of SubString" + " start=1 to end=8" + " String becomes => " + afterRemoval.toString()); } } Output: Before removal String = GeeksforGeeks After removal of SubString start=8 to end=8 String becomes => GeeksforGeeks After removal of SubString start=1 to end=8 String becomes => GGeeks Example 3: To demonstrate IndexOutOfBoundException Java // Java program to demonstrate // exception thrown by the delete() Method. class GFG { public static void main(String[] args) { // create a StringBuilder object // with a String pass as parameter StringBuilder str = new StringBuilder("GeeksforGeeks"); try { // make start greater than end StringBuilder afterRemoval = str.delete(7, 4); } catch (Exception e) { System.out.println("Exception: " + e); } } } Output: Exception: java.lang.StringIndexOutOfBoundsException Reference: https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#delete(int, int) Comment More infoAdvertise with us Next Article StringBuilder delete() in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java java-basics Java-Functions Java-StringBuilder Practice Tags : Java Similar Reads StringBuilder deleteCharAt() in Java with Examples The deleteCharAt(int index) method of StringBuilder class remove the character at the given index from String contained by StringBuilder. This method takes index as a parameter which represents the index of char we want to remove and returns the remaining String as StringBuilder Object. This StringB 3 min read StringBuffer delete() Method in Java with Examples The java.lang.StringBuffer.delete() is an inbuilt method in Java which is used to remove or delete the characters in a substring of this sequence. The substring starts at a specified index start_point and extends to the character at the index end_point. Syntax : public StringBuffer delete(int start_ 3 min read StringBuilder replace() in Java with Examples The replace(int start, int end, String str) method of StringBuilder class is used to replace the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified index start and extends to the character at index end - 1 or to the end of the se 3 min read StringBuffer deleteCharAt() Method in Java with Examples The Java.lang.StringBuffer.deleteCharAt() is a built-in Java method which removes the char at the specified position in this sequence. So that the sequence is reduced by 1 char. Syntax: public StringBuffer deleteCharAt(int indexpoint) Parameters : The method accepts a single parameter indexpoint of 2 min read StringBuilder setLength() in Java with Examples The setLength(int newLength) method of StringBuilder is used to set the length of the character sequence equal to newLength.For every index k greater than 0 and less than newLength. If the newLength passed as argument is less than the old length, the old length is changed to the newLength.If the new 2 min read File delete() method in Java with Examples The delete() function is a part of File class in Java . This function deletes an existing file or directory. If the file is deleted then the function returns true else returns false Function signature: public boolean delete() Syntax: boolean var = file.delete(); Parameters: This method does not acce 2 min read Files delete() method in Java with Examples The delete() method of java.nio.file.Files help us to delete a file located at the path passed as a parameter. This method may not be atomic with respect to other file system operations. If the file is a symbolic link then the symbolic link itself, not the final target of the link, is deleted. If th 3 min read StringBuilder toString() method in Java with Examples The toString() method of the StringBuilder class is the inbuilt method used to return a string representing the data contained by StringBuilder Object. A new String object is created and initialized to get the character sequence from this StringBuilder object and then String is returned by toString( 3 min read KeyStore deleteEntry() method in Java with Examples The deleteEntry(String alias) method of java.security.KeyStore class is used to remove the entry for requested alias name. Syntax: public final void deleteEntry(String alias) throws KeyStoreException Parameter: This method seeks the name of the alias as a parameter of String type of which the entry 3 min read Set clear() method in Java with Examples The Java.util.Set.clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set. Syntax: void clear() Parameters: The me 1 min read Like