473,396 Members | 1,785 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,396 software developers and data experts.

using StringBuilder class for concatenation?

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
4 2600
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
16
by: Stephane | last post by:
Hi, I'm trying to replace parenthesis using Regex.replace but I'm always having this error: System.ArgumentException: parsing ":-)" - Too many )'s. Parameter name: :-) Here's my code: ...
16
by: Alvin Bruney | last post by:
Is string builder intelligent enough to handle concats without behaving like string? Consider myStringBuilder.Append("one" + "two") what does the '+' do here? Because this syntax is also...
14
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in...
11
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
3
by: Mariano | last post by:
Hi, Can a StringBuilder buffer be cleared? I'm confused. It is supposed that the StringBuilder class, can deal with all the memory allocation and deallocation needed for normal string operations...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
5
by: pantagruel | last post by:
Hi, It is generally stated that stringbuilder should be used instead of just concatenating strings with the plus operator. That's fine enough what I'm wondering in cases I have: String S =...
13
by: Tony Johansson | last post by:
Hello! I read in a book and here is a question and the answer that I'm not satisfied with. When should you use the StringBuilder class instead of the String class. 1.When building a string from...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.