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. 3 3500
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.
Thanks to both for the replies. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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:...
|
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...
|
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"...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |