469,267 Members | 1,003 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,267 developers. It's quick & easy.

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 35363
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Rich | last post: by
reply views Thread by FluffyCat | last post: by
4 posts views Thread by James Page | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.