472,352 Members | 1,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 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 9231
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 ....
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...
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
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...
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...
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...
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. ...
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...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

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.