Can a StringBuilder buffer be cleared?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Can a StringBuilder buffer be cleared? I'm confused. It is supposed that the StringBuilder class, can deal with all the memory allocation and deallocation needed for normal string operations (concatenations, replacements, etc.), because the String class just creates and destroys instances at an object level. I can't find a method to clear a StringBuilder object's buffer for reusing it (the idea is to avoid object instantiation/destruction, so I don't want to instantiate several StringBuilder objects).

By the way I prefer code like this:

Dim str as String

str = "->" + Value1 + "<-" + ControlChars.CrLf + "->" + Value2 + "<-" + ControlChars.CrLf

than this:

Dim str as StringBuilder

str.AppendFormat("->{0}<-",Value1)
str.AppendFormat(ControlChars.CrLf)
str.AppendFormat("->{0}<-",Value2)
str.AppendFormat(ControlChars.CrLf)

Finally I will really prefer an escape code for CrLf to be included in the Format method, that will make the use of StringBuilder almost like String. Anyway, I still have missing the Clear method in StringBuilder class...

Did I got something wrong??

Mariano.
 
Mariano said:
Can a StringBuilder buffer be cleared?

sb.length = 0

sets the length to zero so you can reuse the same Stringbuffer.
It doesn't deallocate the buffer.


sb.capacity = 10

sets the buffer size.
 
Mariano,
Can a StringBuilder buffer be cleared? I'm confused.
As Armin stated, sb.Length = 0 is the easiest way.
Finally I will really prefer an escape code for CrLf to be included
in the Format method,
Remember that ControlChars.CrLf is a constant, you can safely include it in
your format string. The concatenation will be done at compile time, not run
time!

Something like:

Const format As String = "->{0}<-" & ControlChars.CrLf & "->{1}<-" &
ControlChars.CrLf

For a "simple" format as above, I normally use String.Format.

Dim str As String
str = String.Format(format, value1, value2)
Did I got something wrong??
Generally I reserve StringBuilder when I am building a really long string,
inside of a loop. For 2 or 3 concatenations such as your example, I find the
pain of StringBuilder not worth the gain.

Of course knowing all three techniques and "profiling" your code to know
when to use one over the other, is the "real" trick. ;-)

Hope this helps
Jay

Mariano said:
Hi,

Can a StringBuilder buffer be cleared? I'm confused. It is supposed that
the StringBuilder class, can deal with all the memory allocation and
deallocation needed for normal string operations (concatenations,
replacements, etc.), because the String class just creates and destroys
instances at an object level. I can't find a method to clear a StringBuilder
object's buffer for reusing it (the idea is to avoid object
instantiation/destruction, so I don't want to instantiate several
StringBuilder objects).
By the way I prefer code like this:

Dim str as String

str = "->" + Value1 + "<-" + ControlChars.CrLf + "->" + Value2 + "<-" + ControlChars.CrLf

than this:

Dim str as StringBuilder

str.AppendFormat("->{0}<-",Value1)
str.AppendFormat(ControlChars.CrLf)
str.AppendFormat("->{0}<-",Value2)
str.AppendFormat(ControlChars.CrLf)

Finally I will really prefer an escape code for CrLf to be included in the
Format method, that will make the use of StringBuilder almost like String.
Anyway, I still have missing the Clear method in StringBuilder class...
 
Back
Top