469,356 Members | 1,958 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 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
Jul 21 '05 #1
5 9143
Rich <Ri**@discussions.microsoft.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)


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
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
"Jon Skeet [C# MVP]" wrote:
Rich <Ri**@discussions.microsoft.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)


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3
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" wrote:
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
"Jon Skeet [C# MVP]" wrote:
Rich <Ri**@discussions.microsoft.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)


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #4
Rich <Ri**@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5
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.
"Rich" <Ri**@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
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" wrote:
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
"Jon Skeet [C# MVP]" wrote:
> Rich <Ri**@discussions.microsoft.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)
>
> 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.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

Jul 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

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