473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Can a StringBuilder buffer be cleared?

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
3 3500
"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
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
Thanks to both for the replies.
Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
20
by: Alvin Bruney | last post by:
On the advice of a user, I've timed stringbuilder v string. Here are the results. Here are the numbers: Total # queries 3747 Time in Milliseconds StringBuilder: String...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
15
by: DV | last post by:
I have a StringBuilder that has a string with 12,000,000 characters. When I do a ToString(), I expect to have ~25,000,000 bytes worth of memory, yet, I end up with ~43,000,000 bytes. That's...
8
by: Henning M | last post by:
Hi, I'm trying to use stringbuilder to collect a list of strings. (as suggested by Claes Bergefall) Declare Auto Function CM_Get_Device_ID_List Lib "cfgmgr32.dll" (ByVal pszFilter As String,...
7
by: Arnd Iffland | last post by:
Hi, i want to use a StringBuilder to create a large string. After instatiation, the MaxCapacity is 2.147.483.647, what is large enough for my purpose. During my procedure i get a...
26
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW:...
8
by: Arjan | last post by:
Hello, I have been looking for a couple of days now, but I can't find anything about how to deal with StringBuilder and releasing the memory used by it. When I use stringbuilder, the memory...
0
by: firstquestion | last post by:
Hello, I need to pass a StringBuilder object to a custom unmanaged DLL. The code most of the time works great, however once in a while I get "Object reference not set to an instance of an object"...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.