how to clear string builder object?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 said:
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)

Personally I'd just create a new StringBuilder for each iteration. It's
not likely to make a significant performance difference, and it's the
most readable way of expressing what you mean, IMO.
 
Thanks for your reply. I am not clear, however, on createing a new
StringBuilder object for each iteration. Here is my interpretation

For i = 0 to 500000 'I have lots of data to read
Dim strData As New StringBuilder
For j = 0 to Columns.Count 'about 180 columns of data
strData.Append colVal(j) & ", "
Next
oWrite.WriteLine(strData.ToString())
Next

Do you think the performance would be the same then as if I did this:

For i = 0 to 500000
For j = 0 to Columns.Count
strData.Append colVal(j) & ", "
Next
oWrite.WriteLine(strData.ToString())
strData.Remove(0, strData.Length)
Next


Thanks
 
I think I get it:

Dim strData As StringBuilder

For i = 0 to 500000 'I have lots of data to read
strData = New StringBuilder
For j = 0 to Columns.Count 'about 180 columns of data
strData.Append colVal(j) & ", "
Next
oWrite.WriteLine(strData.ToString())
Next
 
Rich said:
Thanks for your reply. I am not clear, however, on createing a new
StringBuilder object for each iteration. Here is my interpretation

For i = 0 to 500000 'I have lots of data to read
Dim strData As New StringBuilder
For j = 0 to Columns.Count 'about 180 columns of data
strData.Append colVal(j) & ", "
Next
oWrite.WriteLine(strData.ToString())
Next

Do you think the performance would be the same then as if I did this:

For i = 0 to 500000
For j = 0 to Columns.Count
strData.Append colVal(j) & ", "
Next
oWrite.WriteLine(strData.ToString())
strData.Remove(0, strData.Length)
Next

Yup, probably. It would be better if you did

strData.Append colVal(j)
strData.Append ", "

in the loop though - you wouldn't create as many strings.

Another way of clearing the StringBuilder is to set the length to 0, by
the way.
 
In addition to what Jon said, I would be inclined to go with the following:

For i = 0 to 500000
Dim strData As StringBuilder = New StringBuilder
For j = 0 to Columns.Count - 2
strData.Append colVal(j)
strData.Append ", "
Next
strData.Append colVal(Columns.Count - 1)
oWrite.WriteLine(strData.ToString())
Next

Because Columns is 0 based, using Columns.Count would result in a read past
the the end of the 'Array' thus throwing an exception.

Using Columns.Count - 2 in the loop and then appending the last column
(colVal(Columns.Count - 1)) by itself avoids having a trailing ", " on the
line that is written to oWrite.
 
Back
Top