472,378 Members | 1,299 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #1
5 35797
To clear it:

strData = New StringBuilder

Marcie

On Mon, 18 Apr 2005 09:15:52 -0700, Rich P <rp*****@aol.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)

Thanks,
Rich

*** Sent via Developersdex http://www.developersdex.com ***


Nov 21 '05 #2
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.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #3
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

"Rich P" <rp*****@aol.com> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...
|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
|
| *** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #4
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
Nov 21 '05 #5
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
Nov 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...
5
by: Rich | 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...
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.
0
by: FluffyCat | last post by:
Last fall I started a series of design pattern examples using PHP5. I think the last pattern I did was the Singleton in January. Getting back to it here is my first cut at the Builder pattern. ...
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: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines 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 technical details, Gmail likely implements measures...
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 synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.