By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,693 Members | 1,415 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,693 IT Pros & Developers. It's quick & easy.

using StringBuilder class for concatenation?

P: n/a
Ron
Hello,

I have to concatenate some large strings which end up in a
text file. I am just checking if the StringBuilder class
can improve what I am currently doing - and how to
implement this. Here is what I am currently doing:

Do while...true
...
strData += str1 & ColDelimeter
...
strData += RowDelimeter
oWrite.WriteLine(strData)
strData = ""
Loop

Each row is about 10k in size, average and hundreds of
thousands of rows. Can the StringBuilder Class improve
what I am doing? May I request a sample how to use it?

Thanks,
Ron
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Ron,
| Can the StringBuilder Class improve
| what I am doing?
Yes a StringBuilder can improve what you are doing. However I have to ask
why bother with string concatenation & StringBuilder at all. I would simply
write to the file.

| May I request a sample how to use it? (StringBuilder)

Dim strData As StringBuilder
| Do while...true
' Each row is about 10k in size
strData = New StringBuilder(10 * 1024)
| ...
| strData += str1 & ColDelimeter
strData.Append(str1)
strData.Append(ColDelimeter)
| ...
| strData += RowDelimeter
strData.Append(RowDelimeter)
| oWrite.WriteLine(strData.ToString())
| Loop
| May I request a sample how to use it? (write to the file)

| Do while...true
| ...
| strData += str1 & ColDelimeter
oWrite.Write(str1)
oWrite.Write(ColDelimeter)
| ...
| strData += RowDelimeter
oWrite.Write(RowDelimeter)
| oWrite.WriteLine()
| Loop

Depending on how you opened the file it will be buffered, using the correct
FileStream constructor allows you to increase the size of the buffer.

Be mindful of RowDelimeter, if its CR & LF, WriteLine will write that for
you & you may be doubling your rows.

Hope this helps
Jay

"Ron" <an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
| Hello,
|
| I have to concatenate some large strings which end up in a
| text file. I am just checking if the StringBuilder class
| can improve what I am currently doing - and how to
| implement this. Here is what I am currently doing:
|
| Do while...true
| ...
| strData += str1 & ColDelimeter
| ...
| strData += RowDelimeter
| oWrite.WriteLine(strData)
| strData = ""
| Loop
|
| Each row is about 10k in size, average and hundreds of
| thousands of rows. Can the StringBuilder Class improve
| what I am doing? May I request a sample how to use it?
|
| Thanks,
| Ron
|
|
Nov 21 '05 #2

P: n/a
Yes it most definately will. It will speed it up by an order of magnitude
(in my experience).

Usage is simple:
Dim sb as new StringBuilder

sb = "whatever"
sb = sb + " more whatever"

oWrite.Writeline (sb.ToString)

However, I see you are writing a line at a time (oWrite.WriteLine). If you
have hundreds of thousands of lines, perhaps you should add carriage
return/line feeds with your string build and batch the writes, say, 1000
lines at a time?
"Ron" <an*******@discussions.microsoft.com> wrote in message
news:0c****************************@phx.gbl...
Hello,

I have to concatenate some large strings which end up in a
text file. I am just checking if the StringBuilder class
can improve what I am currently doing - and how to
implement this. Here is what I am currently doing:

Do while...true
...
strData += str1 & ColDelimeter
...
strData += RowDelimeter
oWrite.WriteLine(strData)
strData = ""
Loop

Each row is about 10k in size, average and hundreds of
thousands of rows. Can the StringBuilder Class improve
what I am doing? May I request a sample how to use it?

Thanks,
Ron

Nov 21 '05 #3

P: n/a
Ron
Thanks all for your replies. I think StringBuilder will
help. My deal is that I have to massage each piece of
data that I read from the external source - it contains
all kinds of stuff like formfeed chars, linefeed chars,
etc. I do not use cr & lf chars for rowdelimeter, more
like ||##. The textfile(s) eventually gets sucked up by a
DTS package. So I did not want to take any chances using
delimeters that may be chars that are already in the data
I am retrieving, so I made up my own custom delimeters.
Then I run a DTS package (com) that I translated to VB.Net.

BTW, anyone know when VS2005 is due out? I can't wait to
get my hands on the bulkDataTransfer class. I have been
having some issues using the DTS package in VB6 and
VB.Net.

Anyway, thanks again for the help.

Ron

-----Original Message-----
Hello,

I have to concatenate some large strings which end up in atext file. I am just checking if the StringBuilder class
can improve what I am currently doing - and how to
implement this. Here is what I am currently doing:

Do while...true
...
strData += str1 & ColDelimeter
...
strData += RowDelimeter
oWrite.WriteLine(strData)
strData = ""
Loop

Each row is about 10k in size, average and hundreds of
thousands of rows. Can the StringBuilder Class improve
what I am doing? May I request a sample how to use it?

Thanks,
Ron
.

Nov 21 '05 #4

P: n/a
Hi,

My deal is that I have to massage each piece of
data that I read from the external source
<<

If you have to "massage" then a StringBuilder may not help. StingBuilder is
designed to improve appending (strings are immutable). However, to
manipulate data that is part of a StringBuilder, you have to convert it to a
String. Thus (in general) you loose the performance gain that StringBuilder
provides for the append process. In fact, I expect that your performance
will be much poorer if you use SB.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
www.mabry.com/vbpgser4 to order.
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.