473,587 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String Builder efficiency

I am using a StringBuilder like this:

Dim sb As New StringBuilder

sb.Append("Text field 1: {TXT1}" & VbCrLf)
sb.Append("Text field 2: {TXT2}" & VbCrLf)
sb.Append("Text field 3: {TXT3}" & VbCrLf)

And then later in the code I use this:

Dim sText1Value As String
Dim sText2Value As String
Dim sText3Value As String

sb.Replace("{TX T1}", sText1Value)
sb.Replace("{TX T2}", sText2Value)
sb.Replace("{TX T3}", sText3Value)

My question concerns the length of the string values in the sText1Value
variables. What if their lengths are greater than the tokens ({TXT1},
{TXT2}) that I inserted into the StringBuilder at the beginning?

Will that cause an unnecessary string allocation? Should I make the
tokens as long as the maximum string length that I think will be
replaced? e. g. :

sb.Append("Text field 1: {TXT1LONGTOKENL ENGTH}" & VbCrLf)

Would the StringBuilder still have to make an extra string allocation
in this case or would it just be able to replace the characters in
place? Should I even be concerned about it?

I could not examine the Replace function with ILDASM or Reflector to
see exactly what it did.

Thanks

Nov 21 '05 #1
6 1300
Chris,

you can easily try this code and see the results.
I am not sure what is your concern. Do you have any problems with
sb.Replace?
What do you expect to be maximum length in method call?

As about "average joe" situation, replace 1 character with string of 20
works.

Allocations you can check with any good enough profiler.

HTH
Alex

"Chris Dunaway" <du******@gmail .com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
I am using a StringBuilder like this:

Dim sb As New StringBuilder

sb.Append("Text field 1: {TXT1}" & VbCrLf)
sb.Append("Text field 2: {TXT2}" & VbCrLf)
sb.Append("Text field 3: {TXT3}" & VbCrLf)

And then later in the code I use this:

Dim sText1Value As String
Dim sText2Value As String
Dim sText3Value As String

sb.Replace("{TX T1}", sText1Value)
sb.Replace("{TX T2}", sText2Value)
sb.Replace("{TX T3}", sText3Value)

My question concerns the length of the string values in the sText1Value
variables. What if their lengths are greater than the tokens ({TXT1},
{TXT2}) that I inserted into the StringBuilder at the beginning?

Will that cause an unnecessary string allocation? Should I make the
tokens as long as the maximum string length that I think will be
replaced? e. g. :

sb.Append("Text field 1: {TXT1LONGTOKENL ENGTH}" & VbCrLf)

Would the StringBuilder still have to make an extra string allocation
in this case or would it just be able to replace the characters in
place? Should I even be concerned about it?

I could not examine the Replace function with ILDASM or Reflector to
see exactly what it did.

Thanks

Nov 21 '05 #2
"Chris Dunaway" <du******@gmail .com> schrieb
I am using a StringBuilder like this:

Dim sb As New StringBuilder

sb.Append("Text field 1: {TXT1}" & VbCrLf)
sb.Append("Text field 2: {TXT2}" & VbCrLf)
sb.Append("Text field 3: {TXT3}" & VbCrLf)

And then later in the code I use this:

Dim sText1Value As String
Dim sText2Value As String
Dim sText3Value As String

sb.Replace("{TX T1}", sText1Value)
sb.Replace("{TX T2}", sText2Value)
sb.Replace("{TX T3}", sText3Value)

My question concerns the length of the string values in the
sText1Value variables. What if their lengths are greater than the
tokens ({TXT1}, {TXT2}) that I inserted into the StringBuilder at the
beginning?

Will that cause an unnecessary string allocation? Should I make
the tokens as long as the maximum string length that I think will
be replaced? e. g. :

sb.Append("Text field 1: {TXT1LONGTOKENL ENGTH}" & VbCrLf)

Would the StringBuilder still have to make an extra string
allocation in this case or would it just be able to replace the
characters in place? Should I even be concerned about it?

I could not examine the Replace function with ILDASM or Reflector
to see exactly what it did.

Thanks

I think there is no big difference between adding new chars/strings to the
stringbuilder and replacing parts by longer strings. In both cases it is
possible that the current capacity is exceeded. In general, the initial
capcity I choose for the stringbuilder is about the size I expect the length
will be - unless I have no clue about the size, but this depends on the
case.

BTW, you should use

sb.Append("Text field 1: {TXT1}")
sb.Append(vbCrL f)

instead to avoid unncessary string concatenations.
Armin

Nov 21 '05 #3
Armin Zingler wrote:
BTW, you should use

sb.Append("Text field 1: {TXT1}")
sb.Append(vbCrL f)

instead to avoid unncessary string concatenations.


Surely the compiler would take care of that particular concatenation at
compile time and reduce it to a single string? If there were variables
involved then sure, that would be more efficient as multiple .Append calls.
But when concatenating multiple string literals, I'm sure the compiler
should put these together as a single string in the output code..?

--

(O)enone
Nov 21 '05 #4
"Oenone" <oe****@nowhere .com> schrieb
Armin Zingler wrote:
BTW, you should use

sb.Append("Text field 1: {TXT1}")
sb.Append(vbCrL f)

instead to avoid unncessary string concatenations.


Surely the compiler would take care of that particular concatenation
at compile time and reduce it to a single string? If there were
variables involved then sure, that would be more efficient as
multiple .Append calls. But when concatenating multiple string
literals, I'm sure the compiler should put these together as a
single string in the output code..?

You're right, the compiler recognizes this. I didn't rely on it. ;-)

Armin
Nov 21 '05 #5
Armin,

You're right, the compiler recognizes this. I didn't rely on it. ;-)


Because you was a while not active in this newsgroup. Has been a long and
deep discussed subject.

:-))

Cor
Nov 21 '05 #6
"Cor Ligthert" <no************ @planet.nl> schrieb
Armin,

You're right, the compiler recognizes this. I didn't rely on it.
;-)


Because you was a while not active in this newsgroup. Has been a long
and deep discussed subject.

:-))

Cor


Ah, ok, I didn't know this. :-)

Armin
Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
4682
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, 20-30k), what are the performance differences (if any with something as trivial as this) between initializing a new instance of StringBuilder with a...
0
1581
by: james | last post by:
My fellow VB.NET developers, Ever wonder why MS didn't include a "Class Builder" in Visual Studio .NET like they did in other versions of visual studio? So did we.
11
3890
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 parsed can completely wreck it. The string I am trying to parse is as follows: commandText=insert into (Text) values (@message + N': ' +...
2
5070
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 looking at each char within a loop and copying the char to a stringBuilder instance if current and previous char are not spaces...
8
5408
by: Jami Bradley | last post by:
Hi, I'm looking for an efficient way to do this, because I know it will be heavily used :-) I have a fixed width string and I need to substitute a substring of characters with new values. I can do this with 2 substring calls, but it will need to rebuild the string just to write a few characters. Here is the simple, but inefficient,...
4
1832
by: Sparky Arbuckle | last post by:
I am looping through a listbox collection to build a SQL string that will be used to delete items from a database. I have tried many variances of the code below but have had no luck. The code below gives an error: Cast from type 'ListItem' to type 'String' is not valid. When i do a response.write(s) the item at the top row displays...
4
1911
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 this or point me in the right direction? Thanks
5
1935
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 could be referenced as a string (from a file). I'm not 100% sure of the relationship between the classes, but I suspect that they will not be...
13
7949
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 myDoubleQuote = (char)34; string myDoubleQuote = "" + (char)34;
0
7915
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8205
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8339
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5392
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3872
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2347
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.