Java StringBuilder clear example shows how to clear the contents of StringBuilder or StringBuffer object. This example also shows how to empty StringBuilder/StringBuffer object in Java.
How to clear StringBuilder or StringBuffer in Java?
Suppose you have a StringBuilder object being used inside a loop and you want to empty its contents for each iteration of the loop. In such cases, you can clear the contents of the StringBuilder using below given ways.
1) Clear StringBuilder by assigning a new object
This method assigns a new StringBuilder object having no content to the existing reference. This is not exactly clearing the same object, but assigning a new empty object to the same reference.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class JavaStringBufferClearEmptyExample { public static void main(String[] args) { StringBuilder sbStr = null; for(int i = 1; i <= 5; i++){ //clear the contents from previous iteration sbStr = new StringBuilder(); sbStr.append(i); System.out.print(sbStr); } } } |
Output
1 |
12345 |
2) Clear StringBuilder using the delete method
We can use the delete
method of the StringBuilder class to selectively delete certain characters from it or we can use it to empty the contents of it.
1 |
public void delete(int start, int end) |
The delete
method accepts two parameters, start index and end index. To clear the contents, we are going to provide 0 as the start index and the length of the StringBuilder object as an end index to clear everything it has.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class JavaStringBufferClearEmptyExample { public static void main(String[] args) { StringBuilder sbStr = new StringBuilder(); String strDays[] = {"Sun", "Mon", "Tuesday", "Wed"}; for(int i = 0; i < strDays.length; i++){ //delete all contents from previous iteration sbStr.delete(0, sbStr.length()); sbStr.append(strDays[i]); System.out.print(sbStr); } } } |
Output:
1 2 3 4 |
Sun Mon Tuesday Wed |
As you can see from the output, the content of the StringBuilder is cleared at the start of the loop.
3) Clear StringBuilder using setLength method
You can use the setLength
method to clear the contents of the StringBuilder object.
1 |
public void setLength(int newLength) |
For clearing the contents of the StringBuilder, we are going to pass 0 as new the length as shown in the below example.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class JavaStringBufferClearEmptyExample { public static void main(String[] args) { StringBuilder sbStr = new StringBuilder(); String strDays[] = {"Sun", "Mon", "Tuesday", "Wed"}; for(int i = 0; i < strDays.length; i++){ //empty StringBuilder sbStr.setLength(0); sbStr.append(strDays[i]); System.out.println(sbStr); } } } |
Output:
1 2 3 4 |
Sun Mon Tuesday Wed |
What is the best way to clear the StringBuilder?
Now that we have seen three different methods to clear the StringBuilder, you may have this question. Well, the best way to clear the StringBuilder is to use the setLength
method (3rd option).
Why? Because the first option creates a new StringBuilder object for each iteration of the loop. Object creation is a costly operation because it involves allocating new memory for the new object. Plus, garbage collection has to collect the old unused object. You should avoid this option if the number of iterations is too high due to the performance impact it might have.
Option 2 (delete
method) internally allocates a new buffer with the specified length and then copies the modified contents of the StringBuilder buffer to the new buffer. This is again a costly operation if the number of iteration is too high.
Option 3 is the best choice as it does not involve any new allocation and garbage collection. The setLength
method just resets the internal buffer length variable to 0. The original buffer stays in the memory so new memory allocation is not needed and thus it is the fastest way to clear the StringBuilder.
Read this to know how to check if StringBuilder is empty or other String in Java to know more.
This example is a part of StringBuffer in Java tutorial and StringBuilder in Java tutorial.
References:
StringBuilder JavaDoc
StringBuffer JavaDoc
Thank you so much !!! Very good.
Hi Stephen,
Glad you liked it, thanks.