By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,326 Members | 881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,326 IT Pros & Developers. It's quick & easy.

Can a StringBuilder buffer be cleared?

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Mariano" <an*******@discussions.microsoft.com> schrieb

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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
Nov 20 '05 #2

P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:93**********************************@microsof t.com... 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.

Nov 20 '05 #3

P: n/a
Thanks to both for the replies.
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.