how to clear string builder object?

  • Thread starter Thread starter Rich P
  • Start date Start date
R

Rich P

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

|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
|
|
 
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
 
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
 
Back
Top