string vs stringbuilder

Discussion in 'ASP .Net' started by H.G.Srivatsa, Aug 12, 2005.

  1. H.G.Srivatsa

    H.G.Srivatsa Guest

    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
    H.G.Srivatsa, Aug 12, 2005
    #1
    1. Advertising

  2. 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/?...l/cpconusingstringbuilderclass.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,
    --
    Jeff Hora
    MCSD (.NET & VS6), ITIL Foundations
    Consultant
    Microsoft
    This posting is provided "AS IS" with no warranties, and confers no rights.
    "H.G.Srivatsa" <> wrote in message
    news:ddicjj$l07$...
    > 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], Aug 12, 2005
    #2
    1. Advertising

  3. H.G.Srivatsa

    Mark Rance Guest

    H.G.Srivatsa wrote:
    > 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
    Mark Rance, Aug 13, 2005
    #3
  4. H.G.Srivatsa wrote:
    > 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
    --
    www.richardgrimes.com
    my email is encrypted with ROT13 (www.rot13.org)
    Richard Grimes [MVP], Aug 25, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVzdGlu?=

    inserting a break in a StringBuilder string

    =?Utf-8?B?SnVzdGlu?=, Apr 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    381
    Steve C. Orr [MVP, MCSD]
    Apr 21, 2005
  2. gaidar
    Replies:
    0
    Views:
    361
    gaidar
    May 15, 2005
  3. Replies:
    2
    Views:
    7,052
  4. Jay Balapa
    Replies:
    1
    Views:
    387
    Karl Seguin [MVP]
    Jan 16, 2006
  5. raylopez99
    Replies:
    3
    Views:
    461
    raylopez99
    Jul 2, 2008
Loading...

Share This Page