473,385 Members | 2,162 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 9308
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 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...
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.
5
by: Rich P | 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...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.