473,472 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

StringBuilder size question?

Ron
Hello,

I have to read the contents of a textfile and would like
to read it row by row. Each row contains about 180
fields. Most of the data is varchar, but some text fields
(memo fields). So row size varies from 10k to 50k. If I
declare my stringbuilder var like this:

Dim strData As New StringBuilder(50 * 1024)

am I wasting space? Will this defeat the benefits of
using stringbuilder when a row contains only 5k of data as
I loop through the file? My alternative would be this:

Dim str1 As string, i as integer, arrStr as Array
....'arrStr contains contents of the row
For i = 0 to arrStr.Length - 1
str1 += arrStr.GetValue(i).ToString & ", "
Next

Thanks,
Ron

Nov 21 '05 #1
2 1417
Assigning all the space to a StringBuilder in advance is a great option when
you know how much space you'll need, but falling back to string
concatenation is not your only other option. A StringBuilder will grow as
needed to accomodate whatever content you Append to it, so that should be
your fallback position--just use a StringBuilder (without pre-setting the
size) and Append. That will still be much faster than plain string
concatenation.

Dim strData As New StringBuilder()
Dim i as integer, arrStr as Array
'arrStr contains contents of the row
For i = 0 to arrStr.Length - 1
strData.Append arrStr.GetValue(i).ToString & ", "
Next
"Ron" <an*******@discussions.microsoft.com> wrote in message
news:09****************************@phx.gbl...
Hello,

I have to read the contents of a textfile and would like
to read it row by row. Each row contains about 180
fields. Most of the data is varchar, but some text fields
(memo fields). So row size varies from 10k to 50k. If I
declare my stringbuilder var like this:

Dim strData As New StringBuilder(50 * 1024)

am I wasting space? Will this defeat the benefits of
using stringbuilder when a row contains only 5k of data as
I loop through the file? My alternative would be this:

Dim str1 As string, i as integer, arrStr as Array
...'arrStr contains contents of the row
For i = 0 to arrStr.Length - 1
str1 += arrStr.GetValue(i).ToString & ", "
Next

Thanks,
Ron

Nov 21 '05 #2
Ron
This is great news! Thanks for your reply.

-----Original Message-----
Assigning all the space to a StringBuilder in advance is a great option whenyou know how much space you'll need, but falling back to stringconcatenation is not your only other option. A StringBuilder will grow asneeded to accomodate whatever content you Append to it, so that should beyour fallback position--just use a StringBuilder (without pre-setting thesize) and Append. That will still be much faster than plain stringconcatenation.

Dim strData As New StringBuilder()
Dim i as integer, arrStr as Array
'arrStr contains contents of the row
For i = 0 to arrStr.Length - 1
strData.Append arrStr.GetValue(i).ToString & ", "
Next
"Ron" <an*******@discussions.microsoft.com> wrote in messagenews:09****************************@phx.gbl...
Hello,

I have to read the contents of a textfile and would like
to read it row by row. Each row contains about 180
fields. Most of the data is varchar, but some text fields (memo fields). So row size varies from 10k to 50k. If I declare my stringbuilder var like this:

Dim strData As New StringBuilder(50 * 1024)

am I wasting space? Will this defeat the benefits of
using stringbuilder when a row contains only 5k of data as I loop through the file? My alternative would be this:

Dim str1 As string, i as integer, arrStr as Array
...'arrStr contains contents of the row
For i = 0 to arrStr.Length - 1
str1 += arrStr.GetValue(i).ToString & ", "
Next

Thanks,
Ron

.

Nov 21 '05 #3

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,...
0
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
15
by: DV | last post by:
I have a StringBuilder that has a string with 12,000,000 characters. When I do a ToString(), I expect to have ~25,000,000 bytes worth of memory, yet, I end up with ~43,000,000 bytes. That's...
26
by: Hardy Wang | last post by:
Hi all, I know it is better to handle large string with a StringBuilder, but how does StringBuilder class improve the performance in the background? Thanks! -- WWW:...
7
by: rhkodiak | last post by:
I was in an interview question what is the most efficient way to sort a string for example "010101", the result would then be "000111", and write the code on how to do this. I wrote the code for...
34
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding...
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
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,...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. 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.