Three Ways to Clear a StringBuilder
Here’s a micro-pattern that can show up sometimes in string processing: a StringBuilder is created, and then in a loop some sort of string is built with repeated Append() calls (or with the much under-used AppendFormat() method). The string is then pulled out and consumed by something else, the StringBuilder is emptied and re-used on the next iteration.
For some reason there is no Clear() method on a StringBuilder. I always thought this odd. I’ve generally used:
someStringBuilder.Remove(0,someStringBuilder.Length);
Today, trying to tighten something time-critical, I ran across:
string theString = someStringBuilder.ToString(); someStringBuilder.Replace(theString,"");
I was about to replace this with my usual Remove() call, figuring that eliminating the string matching would be a performance win, when on a sudden impulse I tried this:
someStringBuilder.Length = 0;
Lo and behold, the Length property is not read-only. No wonder there is no Clear() method; one isn’t really needed, except maybe to make how to do this more obvious.
3 Comments »
RSS feed for comments on this post. TrackBack URI
Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Nice tip(s).
Did you check the memory after a garbage collection to see if it really saves anything?
Bob responds: No, because the point isn’t to save memory, it’s to clear a string builder for re-use in a way that is simpler. If you need to clear it, you need to clear it. In addition, you beg the question, “saves anything in comparison to what?” Saves time vs newing up a new StringBuilder? Probably. Saves time vs the other methods I mentioned? Very likely. Enough of either to matter? Depends on the particular situation and the mix of factors such as how much other work is being done within the loop, the size of the strings being manipulated, and so on. But I subscribe to the principle that the drop-dead simplest way of doing any one thing tends to be the very best way of doing that thing, even if the only obvious benefit is elegant, self-evident code.
If you mean, does setting the length to zero clear the buffer and free any memory … I doubt it. A StringBuilder has a default initial capacity of 16 byes (you can override the default with one of the overloaded constructors). If the string exceeds the capacity, the internal buffer is doubled in size until it’s big enough. I’m sure that setting the length to zero simply makes the buffer re-usable, and doesn’t shrink it. However, in most cases, freeing up memory would not be a net win because as the new string is built, the buffer would have to be re-expanded, which is a fairly expensive operation (and for all I know, tends to fragment memory).
Comment by decebal — August 16, 2007 @ 7:23 am
FYI, .NET 3.5 framework introduces StringBuilder.Clear()
Comment by Ahmed — February 27, 2008 @ 11:40 am
Ahmed ….where ? I don’t see .Clear() coming up in a newly created solution in VS2008 using 3.5. What assembly/version are you loading that includes that interface for StringBuilder ?
Comment by Marcelo Lopez — July 24, 2008 @ 12:33 pm