473,473 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to clear string builder object?

I have to read data from an external source, massage the data,
concatenate it to one long string, then write it to a textfile. So I am
experimenting with the StringBuilder object. I append the data in a
loop, then I write the data
to text file, then I need to clear the StringBuilder object for the next
row of data. First, is this the proper use of the StringBuilder object?
Second, I am clearing the object using the Remove method starting at
point 0 and
StringBuilderObject.Length. Is this correct for clearing the object?

Imports System.Text
...
Dim strData As New StringBuilder
...
strData.Append ....
...
strDate.Remove(0, strData.Length)

Thanks,
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #1
5 35956
To clear it:

strData = New StringBuilder

Marcie

On Mon, 18 Apr 2005 09:15:52 -0700, Rich P <rp*****@aol.com> wrote:
I have to read data from an external source, massage the data,
concatenate it to one long string, then write it to a textfile. So I am
experimenting with the StringBuilder object. I append the data in a
loop, then I write the data
to text file, then I need to clear the StringBuilder object for the next
row of data. First, is this the proper use of the StringBuilder object?
Second, I am clearing the object using the Remove method starting at
point 0 and
StringBuilderObject.Length. Is this correct for clearing the object?

Imports System.Text
..
Dim strData As New StringBuilder
..
strData.Append ....
..
strDate.Remove(0, strData.Length)

Thanks,
Rich

*** Sent via Developersdex http://www.developersdex.com ***


Nov 21 '05 #2
Thanks. Just to make sure here is what I am planning on doing:

Dim strData As StringBuilder

For i = 1 to 500000
strData = New StringBuilder
For j = 0 to ColVal.Length
strData.Append ColVal(j) & ", "
Next
oWrite.WriteLine(strData.ToString())
Next

If you see any issues with this - please advise.

Thanks for your help.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #3
Rich,
As Marcie suggests its easier to create a new StringBuilder when you need to
"clear" it.

Depending on the size and "ownership" of StringBuilders involved I will
consider setting the length of the StringBuilder to zero rather then
creating a new one. This potentially "limits" the amount of work the GC
needs to do as you are limiting the amount of objects created, however it
also may cause more work for the GC, as the single instance of the
StringBuilder may be promoted to generation 1 or 2...

Something like:

| Dim strData As StringBuilder
|
| For i = 1 to 500000
strData.Length = 0
| For j = 0 to ColVal.Length
| strData.Append ColVal(j) & ", "
| Next
| oWrite.WriteLine(strData.ToString())
| Next

Which is "Better" (length = 0 or new stringbuilder) really depends on the
dynamics of each individual program...
However! in your example I don't think I would bother with a StringBuilder,
rather I would simply write to the output file directly.

| For i = 1 to 500000

| For j = 0 to ColVal.Length
oWrite.Write(ColVal(j))
oWrite.Write(",")
| Next
| oWrite.WriteLine()
| Next

Hope this helps
Jay

"Rich P" <rp*****@aol.com> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...
|I have to read data from an external source, massage the data,
| concatenate it to one long string, then write it to a textfile. So I am
| experimenting with the StringBuilder object. I append the data in a
| loop, then I write the data
| to text file, then I need to clear the StringBuilder object for the next
| row of data. First, is this the proper use of the StringBuilder object?
| Second, I am clearing the object using the Remove method starting at
| point 0 and
| StringBuilderObject.Length. Is this correct for clearing the object?
|
| Imports System.Text
| ..
| Dim strData As New StringBuilder
| ..
| strData.Append ....
| ..
| strDate.Remove(0, strData.Length)
|
| Thanks,
| Rich
|
| *** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #4
Rich,

By this kind of problems do I always looks how does what is the effect on my
program.

You will see that probably 80% of the time is taken by 20% of the code.

While in general that in that 20% is not the destruction of objects, what
should in normal situations find place when your program is, in let say in a
kind of idle state. (It is waiting on a new key press or something).

As far as I noticed goes it extremely fast. I would only look to it when it
becomes real a problem. What I never saw with the destruction of a
stringbuilder object by the way.

Don't forget it is not removing the data, it is only changing, setting or
whatever pointers.

Just my thought,

Cor
Nov 21 '05 #5
doh,

By this kind of problems do I always looks how does what is the effect on
my program.

By this kind of problems do I always look what is the effect on my program.

To much changed.

Cor
Nov 21 '05 #6

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

Similar topics

3
by: Dave Byron | last post by:
I am having trouble with DBNull's from my SQL server. I am building/converting a asset web app from Access and my db has nulls on various fields . I have tried searching newsgroups and tried to get...
5
by: Rich | last post by:
I have to read data from an external source, massage the data, concatenate it to one long string, then write it to a textfile. So I am experimenting with the StringBuilder object. I append the...
11
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
2
by: José Joye | last post by:
Hello, I was wondering if there is a method that exists to replace multi-spaces within a string with single-space. eg: "12 3 4 56" --> "12 3 4 56" I think this could be done by...
12
by: Tee | last post by:
String Builder & String, what's the difference. and when to use which ? Thanks.
0
by: FluffyCat | last post by:
Last fall I started a series of design pattern examples using PHP5. I think the last pattern I did was the Singleton in January. Getting back to it here is my first cut at the Builder pattern. ...
3
by: rsine | last post by:
I have searched around a little and have yet to find a naming convention for the string builder object. I really do not want to use "str" since this is for string objects and thus could be...
4
by: James Page | last post by:
Hi all I have a shopping cart object which I'd like to send the contents via an e-mail. I get an error saying 'hybridDictionary' cannot be converted to string. Does anyone know how to do...
5
by: TazaTek | last post by:
Hello, I've seen some vague references on how to do this a factory, but not in enough detail to create one, or even know if it's what I need. Essentially, I'll have one of about 5 classes that...
13
by: xzzy | last post by:
None of the following properly do the VB.net double quote conversion because all of the following in csharp convert to \" instead of just a double quote: " I have tried: char...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.