473,727 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String Builder & String, what's the difference ?

Tee
String Builder & String, what's the difference.
and when to use which ?
Thanks.
Nov 18 '05 #1
12 2454
On Fri, 25 Jun 2004 10:24:00 +0800, Tee <th*@streamyx.c om> wrote:
String Builder & String, what's the difference.
and when to use which ?
Thanks.


Basically String is fine for never-changing values; but if you create a
'string-like' variable and need to change it's value (such as
concatenating a string value together) use StringBuilder:

http://msdn.microsoft.com/library/en...ilderclass.asp
--
Craig Deelsnyder
Microsoft MVP - ASP/ASP.NET
Nov 18 '05 #2
Strings are immutable. Meaning if you try to: string1 = string1 + string2,
it needs to create a new memory location for string1 and set the original
location for GC.

StringBuilder does not behave this way.

Don

"Tee" <th*@streamyx.c om> wrote in message
news:#U******** ******@tk2msftn gp13.phx.gbl...
String Builder & String, what's the difference.
and when to use which ?
Thanks.

Nov 18 '05 #3
Tee,

Here's my take on it :

Use stringbuilder whenever you need to do LOTS of string manipulations. For
quick/little things, I don't bother. So if I'm just adding a string in one
line I would do something like this --

strA = strB & strC

However, I had a loop where I was doing about 1,200 concatinations. Using
string builder reduced the time this took substantially.

Others may feel different, but this works for me.

-Saul
"Tee" <th*@streamyx.c om> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
String Builder & String, what's the difference.
and when to use which ?
Thanks.

Nov 18 '05 #4
> in a few words - stringbuilder is much faster! use it as often as you can
instead

Odd that Microsoft doesn't recommend it that way...

Once a StringBuilder class is instantiated, it is indeed much faster.
However, the instantiation of an Object is costly in itself. A String is a
primitive, which means that unless you treat it as an object, it is
certainly much faster to use than an Object (which is why the .Net framework
includes primitives). In addition, when you instantiate a StringBuilder, it
allocates the default amount of Memory needed to hold its entire buffer (the
buffer is how it avoids re-allocating Memory). Therefore, if you are doing a
small amount of work with a string, it may indeed more efficient NOT to use
a StringBuilder. Consider the following:

StringBuilder s = new StringBuilder(" Hello Mom").;
s.Append(", I mean, Mother");
Response.Write( s.ToString());

string s = "Hello Mom";
s += ", I mean, Mother";
Response.Write( s);

Which runs faster? Which uses the most Memory?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"ISK" <IS*@discussion s.microsoft.com > wrote in message
news:43******** *************** ***********@mic rosoft.com...
in a few words - stringbuilder is much faster! use it as often as you can instead of the regular string especially if you are doing a lot of string manipulations.
a little more in depth - a string is 'unchangeable' in the sense that once you've assigned a value to it, it never changes; any modifications you make to it like say adding more characters, appending a string or trimming it etc will create a new string object and assign the modified value to it and discard the old string object leaving it for the GC to take care of during its collection routines.. so a lot of string manipulation means a lot of allocation de-allocation of memory which isn't very efficient..obvi ously this also means that the methods like s3 = s1 & s2 are slower than you would expect..

on the other hand, the stringbuilder creates a sort of buffer (default is 128 chars but u can change it with its overloaded constructor) and whenever you use the 'append' method, it adds the appended character(s) or string to the created buffer...thus there is no creation-destruction of objects on the fly which means its much faster..

just to give you an idea..the operation s1 = s1 & s2 is almost 100 times slower than stringbuilder1 = stringbuilder1. append(s2) (assuming s1 and stringbuilder1 are of same lengths) even for the smallest of lengths of s1 and s2 (run the operations in a loop of say like a hundred thousand to notice the difference)
hope this helps..

"Tee" wrote:
String Builder & String, what's the difference.
and when to use which ?
Thanks.

Nov 18 '05 #5
On Fri, 25 Jun 2004 13:24:21 -0400, Kevin Spencer wrote:
in a few words - stringbuilder is much faster! use it as often as you can instead

Odd that Microsoft doesn't recommend it that way...


They do.

http://msdn.microsoft.com/library/de...etperftips.asp

see the section: Use StringBuilder for Complex String Manipulation
Once a StringBuilder class is instantiated, it is indeed much faster.
However, the instantiation of an Object is costly in itself. A String is a
primitive, which means that unless you treat it as an object, it is
Not true. Strings are reference types (not "primitives ). The only reason
a string MAY be faster to instantiate is because it doesn't allocate a
buffer, it simply allocates a 4-byte pointer and sets it to null. Where as
a stringbuilder will allocate an initial buffer (though the default buffer
size is only sixteen characters).
certainly much faster to use than an Object (which is why the .Net framework
includes primitives).
Strings are objects.
In addition, when you instantiate a StringBuilder, it
allocates the default amount of Memory needed to hold its entire buffer (the
The initial buffer (unless specified) is only sixteen characters...
buffer is how it avoids re-allocating Memory). Therefore, if you are doing a
small amount of work with a string, it may indeed more efficient NOT to use
a StringBuilder. Consider the following:

StringBuilder s = new StringBuilder(" Hello Mom").;
s.Append(", I mean, Mother");
Response.Write( s.ToString());

string s = "Hello Mom";
s += ", I mean, Mother";
Response.Write( s);

Which runs faster? Which uses the most Memory?


For this example, a stringbuilder would be overkill. But, put it in a loop
or do more then 15 or 20 appends on the string... StringBuilder will
probably come out the winner in both speed and memory - because with a
regular string, there are temporary string objects being created in the
background when you concatenate.

--
Tom Shelton [MVP]
Nov 18 '05 #6
Tom, Tom, Tom. Note the exception that I made. You stated (and I quoted, and
now quote):
in a few words - stringbuilder is much faster! use it as often as you
can
instead
By your own admission, my example proves this statement to be false:
Which runs faster? Which uses the most Memory?
For this example, a stringbuilder would be overkill.


I'm not into debate. Just trying to clear the air.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Tom Shelton" <to*@YOUKNOWTHE DRILLmtogden.co m> wrote in message
news:1c******** *************** *****@40tude.ne t... On Fri, 25 Jun 2004 13:24:21 -0400, Kevin Spencer wrote:
in a few words - stringbuilder is much faster! use it as often as you
can instead

Odd that Microsoft doesn't recommend it that way...

They do.

http://msdn.microsoft.com/library/de...etperftips.asp
see the section: Use StringBuilder for Complex String Manipulation
Once a StringBuilder class is instantiated, it is indeed much faster.
However, the instantiation of an Object is costly in itself. A String is a primitive, which means that unless you treat it as an object, it is
Not true. Strings are reference types (not "primitives ). The only reason
a string MAY be faster to instantiate is because it doesn't allocate a
buffer, it simply allocates a 4-byte pointer and sets it to null. Where

as a stringbuilder will allocate an initial buffer (though the default buffer
size is only sixteen characters).
certainly much faster to use than an Object (which is why the .Net framework includes primitives).
Strings are objects.
In addition, when you instantiate a StringBuilder, it
allocates the default amount of Memory needed to hold its entire buffer (the
The initial buffer (unless specified) is only sixteen characters...
buffer is how it avoids re-allocating Memory). Therefore, if you are
doing a small amount of work with a string, it may indeed more efficient NOT to use a StringBuilder. Consider the following:

StringBuilder s = new StringBuilder(" Hello Mom").;
s.Append(", I mean, Mother");
Response.Write( s.ToString());

string s = "Hello Mom";
s += ", I mean, Mother";
Response.Write( s);

Which runs faster? Which uses the most Memory?


For this example, a stringbuilder would be overkill. But, put it in a

loop or do more then 15 or 20 appends on the string... StringBuilder will
probably come out the winner in both speed and memory - because with a
regular string, there are temporary string objects being created in the
background when you concatenate.

--
Tom Shelton [MVP]

Nov 18 '05 #7
Hi Tom,

I was always using
dim sb as new stringbuilder
sb.append("Hell o")
sb.append(" how ")
sb.append("are you?")

Than Fergus ask me once, why you do that, this is overkill? I tested it,
and the difference was almost nothing, and therefore I stopped to show it
this way forever (with long strings I always show/do it like above) and did
it with shortstrings as above no more. Then some weeks ago, Jay B stated to
me, that it was faster to do it as above. (You understand it probably, the
fire was on).

To be sure I tested it again in a very long loop, making a long string from
it and making this as short strings (however 100000 times or so). It is
always faster than concatenating the string, so I use this above again.

:-)

Cor
Nov 18 '05 #8
Cor,
it with shortstrings as above no more. Then some weeks ago, Jay B stated to me, that it was faster to do it as above. (You understand it probably, the
fire was on). Please do not take things I say out of context!

I'm not sure which thread you think I told you that, but its obvious to me
you are mis-quoting me!

Thanks
Jay

"Cor Ligthert" <no**********@p lanet.nl> wrote in message
news:uB******** ******@TK2MSFTN GP11.phx.gbl... Hi Tom,

I was always using
dim sb as new stringbuilder
sb.append("Hell o")
sb.append(" how ")
sb.append("are you?")

Than Fergus ask me once, why you do that, this is overkill? I tested it,
and the difference was almost nothing, and therefore I stopped to show it
this way forever (with long strings I always show/do it like above) and did it with shortstrings as above no more. Then some weeks ago, Jay B stated to me, that it was faster to do it as above. (You understand it probably, the
fire was on).

To be sure I tested it again in a very long loop, making a long string from it and making this as short strings (however 100000 times or so). It is
always faster than concatenating the string, so I use this above again.

:-)

Cor

Nov 18 '05 #9
Hi Jay,
Please do not take things I say out of context! I'm not sure which thread you think I told you that, but its obvious to me
you are mis-quoting me!


Who wrote this message that was added to a message for an OP what I had
specially kept simple for the OP. However Jay B Harlow had again to show
that I was wrong and putted it in a way if I did not know that there where
better ways.

http://www.google.com/gr************...TNGP10.phx.gbl

I hope this helps?

Cor
Nov 18 '05 #10

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

Similar topics

37
4718
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 specified capacity vs. initializing a new instance without... (the final length is not fixed) ie,
13
1175
by: Tee | last post by:
String Builder & String, what's the difference. and when to use which ? Thanks.
33
4681
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 of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation is when the number of string concatunation is more then N ( N varies between 3 to max 15 from...
26
3197
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: http://hardywang.1accesshost.com ICQ: 3359839 yours Hardy
7
1432
by: simonZ | last post by:
I have array variable transfer: String transfer which has some data Than I would like to convert this data into string: Which way is more efficient: StringBuilder rezult=new StringBuilder(); for (int i = 0; i < transfer.Length; i++) {
0
8891
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9406
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9121
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8106
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6703
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6012
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3229
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
3
2158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.