try this

    int i = ddd.indexOf(aaa);
    if (i != -1) {
        ddd.delete(i, i + aaa.length());
    }
Answer from Evgeniy Dorofeev on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ stringbuilder-delete-in-java-with-examples
StringBuilder delete() in Java with Examples - GeeksforGeeks
January 9, 2026 - The delete(int start, int end) method of the StringBuilder class removes a sequence of characters from the string represented by the StringBuilder object.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ stringbuilder โ€บ .delete()
Java | StringBuilder | .delete() | Codecademy
August 22, 2022 - The .delete() method removes a substring from the contents of a StringBuilder and returns the modified object. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ clearing a stringbuilder or stringbuffer
Clearing a StringBuilder or StringBuffer | Baeldung
January 27, 2024 - The method setLength updates the inner length of the StringBuilder. All entries after the length are then ignored when manipulating the StringBuilder.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ lang โ€บ stringbuilder_delete.htm
Java StringBuilder delete() Method
public class StringBuilderDemo { public static void main(String[] args) { try { //create an object of the StringBuilder StringBuilder sb = new StringBuilder("JavaProgramming"); 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); } } }
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ remove the last character of a java stringbuilder
Remove the Last Character of a Java StringBuilder | Baeldung
January 8, 2024 - The StringBuilder class has the deleteCharAt() method. It allows us to remove the character at the desired position.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ stringbuilder-deletecharat-in-java-with-examples
StringBuilder deleteCharAt() in Java with Examples - GeeksforGeeks
January 9, 2026 - The deleteCharAt(int index) method of Java's StringBuilder class is used to remove a character at a specified position in a string.
๐ŸŒ
Kode Java
kodejava.org โ€บ how-do-i-remove-substring-from-stringbuilder
How do I remove substring from StringBuilder? - Learn Java by Examples
May 17, 2023 - This example demonstrate you how to use the StringBuilder delete(int start, int end) and deleteCharAt(int index) to remove a substring or a single character from a StringBuilder.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 03 โ€บ how-to-remove-first-and-last-character-from-String-in-java-example.html
How to Remove First and Last Character of String in Java - Example Tutorial
The substring() method is overloaded ... in Java. Alternatively, you can convert String to StringBuffer or StringBuilder and then use its remove() method to remove the first or last character....
Top answer
1 of 3
3

A practical reason is that it makes things easier in some common situations:

  • If you want the operation to include everything until the end of the string, you can directly use the length as endIndex
  • If you have "separation characters", like the dot between base name and filetype suffix, you typically don't want to include them, so you can use the index of the separation character as endIndex, e.g. String basename = filename.substring(0, filename.indexOf('.'))
2 of 3
1

I agree with Micheal Borgwardt's answer but there is an opportunity for elaboration. The approach you see here is not only useful but also consistent with most the APIs you will find in Java and many other languages. The association with 0-based indexing is more clear if you think about the standard old-style for-loop: for (int i = 0; i < end; i++). These days we tend to favor for-each style constructs but this is how a lot of code is written. Note the while condition, it's based on and exclusive end with a < instead of a <=. When you are writing a lot of loops like this, it's helpful to use a similar construct each time.

But to really understand the bigger picture on this, I think it helps to look at a different kind of problem: time intervals. Let's imagine I want to tell if something happened today, in the morning. If I structure this as an inclusive end, I then have a problem of figuring out what the last time possibly could be. That means I need to know the smallest resolution of the clock. Is it seconds, milliseconds, microseconds, nanoseconds? Let's say I think seconds is good enough. So I say 'less than or equal to 11:59'. Does that work if it happened at 11:59 and 30 seconds? Well, I'll check whether "the minute of the day was less than or equal to 11:59". Or I could just say "less than noon" which is a lot more elegant and tends to be more robust.

Or consider date periods. How do I know if two date periods are adjacent i.e. that there is no gap in between? If I used an exclusive end, it's trivial. I just check that the (exclusive) end on the first period is equal to or after the (inclusive) start of the second. If I use an inclusive end, now I have to get out a calendar and figure out if the day after September 30th is October 1st. And does March 1st follow Feb 28? What year is it? Divide that by 4 unless we are talking about the last year of the century but don't forget that the year 2000 is the exception to the exception. There are strategies to solve that kind of thing but the easiest is to convert the inclusive end to exclusive.

๐ŸŒ
W3docs
w3docs.com โ€บ home โ€บ code snippets โ€บ java โ€บ remove last character of a stringbuilder?
Remove last character of a StringBuilder? | W3docs
To remove the last character of a StringBuilder in Java, you can use the deleteCharAt method and pass in the index of the character you want to delete.
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ stringbuilder โ€บ .deletecharat()
Java | StringBuilder | .deleteCharAt() | Codecademy
August 22, 2022 - The .deleteCharAt() method removes the character at the specified index from the contents of a StringBuilder and returns the modified object. ... Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more! ... Learn to code in Java ...
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-stringbuilder-delete
Java StringBuilder.delete() - Syntax & Examples
January 24, 2021 - StringBuilder.delete() removes the characters in a substring of this sequence and returns reference to this StringBuilder object.
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ remove last character of a stringbuilder?
Remove last character of a StringBuilder? | Codemia
January 26, 2025 - Unlike strings, which are immutable ... modifications like concatenation. To remove the last character from a StringBuilder, you can use the deleteCharAt() method or the setLength() method....
๐ŸŒ
Medium
medium.com โ€บ @avihaibinder โ€บ best-practice-to-clear-a-stringbuilder-in-java-2961fc8084de
Best practice to clear a StringBuilder in Java | by Avihai Binder | Medium
June 25, 2023 - Beware that if you donโ€™t free the StringBuilder space after you done with it, using setLength(0) may result a memory leak, so please select wisely your scope where you choose to use your StringBuilder and be extra picky with the your references.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ remove-first-and-last-character-of-a-string-in-java
Remove first and last character of a string in Java - GeeksforGeeks
To do so, firstly create a StringBuilder object sb for the given string. Remove the first and last character using sb.deleteCharAt(0) and sb.deleteCharAt(str.length() - 1) respectively.
Published: July 15, 2025