There is a member that allows it, but what might be surprising at first is that it isn't a method, but a property.
Set "Length" to zero. Length is writeable in the StringBuilder class. The internal buffer will be untouched (which is good for performance), and subsequent "Appends" will overrwrite the old contents.
StringBuilder already must keep the allocated buffer length distinct from the in-use length. Making the property read/write just takes it to the next logical step. If you are repeatedly building up a StringBuilder to the same length and resetting it, you will find that after the first cycle there will be no addition heap allocations. Good performance is probably just what you are after (or you would have just allocated a new StringBuilder in your code in the first place instead of looking for a way to reset its contents).
(You might be curious what happens if you set the length to a value *higher* than the original length. In that case, the additional space is filled with nulls.)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.