Home » Java » How can I clear or empty a StringBuilder? [duplicate]

How can I clear or empty a StringBuilder? [duplicate]

Posted by: admin November 2, 2017 Leave a comment

Questions:

This question already has an answer here:

Answers:

Edit Updated to bring in feedback.

So two ways that work:

  1. Use stringBuilderObj.setLength(0).
  2. Allocate a new one with new StringBuilder() instead of clearing the buffer
Questions:
Answers:

There are basically two alternatives, using setLength(0) to reset the StringBuilder or creating a new one in each iteration. Both can have pros and cons depending on the usage.

If you know the expected capacity of the StringBuilder beforehand, creating a new one each time should be just as fast as setting a new length. It will also help the garbage collector, since each StringBuilder will be relatively short-lived and the gc is optimized for that.

When you don’t know the capacity, reusing the same StringBuilder might be faster. Each time you exceed the capacity when appending, a new backing array has to be allocated and the previous content has to be copied. By reusing the same StringBuilder, it will reach the needed capacity after some iterations and there won’t be any copying thereafter.

Questions:
Answers:

delete is not overly complicated :

myStringBuilder.delete(0, myStringBuilder.length());

You can also do :

myStringBuilder.setLength(0);

Questions:
Answers:

If you look at the source code for a StringBuilder or StringBuffer the setLength() call just resets an index value for the character array. IMHO using the setLength method will always be faster than a new allocation. They should have named the method ‘clear’ or ‘reset’ so it would be clearer.

Questions:
Answers:

I’ll vote for sb.setLength(0); not only because it’s one function call but because it don’t actually copy the array into another array like sb.delete(0, builder.length());, It just fill the remaining characters to be 0 and set the length variable to the new length.

You can take a look into their implementation to validate my point from here at setLength function and delete0 function.

Questions:
Answers:
StringBuilder s = new StringBuilder();
s.append("a");
s.append("a");
// System.out.print(s); is return "aa"
s.delete(0, s.length());
System.out.print(s.length()); // is return 0

is the easy way.

Questions:
Answers:

you should use sb.delete(0, sb.length()) or sb.setLength( 0 ) and NOT create a new StringBuilder().

see related post for performance Is it better to reuse a StringBuilder in a loop?

Questions:
Answers:

If performance is the main concern … the irony (imo) is the java constructs to format the text that goes into the buffer will be far more time consuming on the CPU than the alloc/realloc/garbage collection … well, possibly not the GC depending on how many builders you create and discard.

But simply appending a compound string (“Hello World of ” + 6E9 + ” earthlings.”) to the buffer is likely to make the whole matter inconsequential.

And, really, if StringBuilder’s involved the content is complex and lengthy and longer than a simple String str = “Hi”; (nevermind java probably uses a builder in the background anyway)

Personally, I try not to abuse the GC. So if it’s something that’s going to be used a lot in rapid fire scenario – like, say, writing debug output messages … I just assume declare it elsewhere and zero it out for reuse.

class MyLogger {
    StringBuilder strBldr = new StringBuilder(256);

    public LogMsg( String stuff ) {

        strBldr.setLength(0);

        // ... prepend status level
        strBldr.append("Info");
        // ... prepend timestamp
        strBldr.append(" " + getTimestamp());
        // ... user msg
        strBldr.append(":" + msg);

        log.write(strBldr.toString());
    }
}

Questions:
Answers:

I think many of the answers here may be missing a quality method included in StringBuilder: .delete(int start, [int] end). I know this is a late reply; however, this should be made known (and explained a bit more thoroughly).

Let’s say you have a StringBuilder table – which you wish to modify, dynamically, throughout your program (one I am working on right now does this), e.g.

StringBuilder table = new StringBuilder();

If you are looping through the method and alter the content, use the content, then wish to discard the content to “clean up” the StringBuilder for the next iteration, you can delete it’s contents, e.g.

table.delete(int start, int end). 

start and end being the indices of the chars you wish to remove. Don’t know the length in chars and want to delete the whole thing?

table.delete(0, table.length());

NOW, for the kicker. StringBuilders, as mentioned previously, take a lot of overhead when altered frequently (and can cause safety issues with regard to threading); therefore, use StringBuffer – same as StringBuilder (with a few exceptions) – if your StringBuilder is used for the purpose of interfacing with the user.