Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

string vs stringbuilder

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
H.G.Srivatsa - 12 Aug 2005 15:43 GMT
Hello Experts
I'm very new to software field....
Can any1 send me the advantages of Stringbuilder class over String class

Thanks in advance
Vatsa
Jeff Hora [MSFT] - 12 Aug 2005 17:28 GMT
Hi Vatsa,

A good place to take a look at what StringBuilder can do is the overview
page on it in the MSDN library "Using the StringBuilder Class" which can be
found at
http://msdn.microsoft.com/library/?url=/library/en-us/cpguide/html/cpconusingstr
ingbuilderclass.asp?frame=true
.
The main thing I've found to be the advantage of StringBuilder is it makes
string manipulation less resource intensive.

HTH

Regards,
Signature

Jeff Hora
MCSD (.NET & VS6), ITIL Foundations
Consultant
Microsoft
This posting is provided "AS IS" with no warranties, and confers no rights.

> Hello Experts
> I'm very new to software field....
> Can any1 send me the advantages of Stringbuilder class over String class
>
> Thanks in advance
> Vatsa
Mark Rance - 13 Aug 2005 07:24 GMT
> Hello Experts
> I'm very new to software field....
> Can any1 send me the advantages of Stringbuilder class over String class
>
> Thanks in advance
> Vatsa

the string class is immutable and therefore very inefficient if you need
to change it once it is constructed.  For those cases, StringBuilder is
almost always a better choice

-Mark
Richard Grimes [MVP] - 25 Aug 2005 14:55 GMT
> Hello Experts
> I'm very new to software field....
> Can any1 send me the advantages of Stringbuilder class over String
> class

String is immutable which means that it cann be changed. Strings are
also interned, which means that if two string variables have the same
collection of characters then they are the same string object. This is
more efficient with runtime memory, and it means that the literal
strings take up less space in the application's executable file.

Since String is immutable it means that when you do any string
manipulation (eg ToLower, ToUpper etc) then a new String is created with
the results. Indeed something like this:

string s = "a";
s += "b";

Looks like the string s has the character b added to its end. In fact
this is not the case, another string is created, "ab", and s is assigned
to it. You can imagine that if you do a lot of concatenation then a lot
of string objects are created. These will *eventually* be released by
the garbage collector, but 'eventually' could be some time, during which
your application's memory usage goes up and up.

StringBuilder is designed to allow you to pre-allocate some memory and
then assign characters to it. StringBuilder will monitor the usuage of
the memory buffer and if it needs more it will automatically
re-allocate. Of course, if this happens then the old memory will stay
around until the garbage collector releases it. However, careful
analysis of your code should allow you to determine the amount of memory
you need. Once the you have finished constructing your string you can
tell StringBuilder to create an immutable String from it.

Another aspect (which I won't go in to details, just mention in passing)
is managed/unmanaged code interoperation. If the parameter of an
unmanaged function is a fixed (const) string then you can pass a String.
The interop layer will do conversion to ANSI string if necessary.
However, if you want the unmanaged function to fill a string buffer to
return a string you should not use a String, because it is immutable.
Instead, you can pre-allocate a StringBuilder (define its capacity) and
pass that to the unmanaged function. The interop layer understands
StringBuilder and treats it appropriately. When the unmanaged function
returns you use StringBuilder.ToString() to get access to the string.

Richard
Signature

www.richardgrimes.com
my email evpuneqt@zicf.bet is encrypted with ROT13 (www.rot13.org)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.