Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to replace string with StringBuilder

How to replace string with StringBuilder

Scheduled Pinned Locked Moved C#
helpquestioncsharpdotnetxml
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aseem Sharma
    wrote on last edited by
    #1

    Hi, In my application I am very much using the System.string class for every manipulation of characters (Reading a lot of data from a XML file, writing a lot of data in a XML file and creating log for each operation). If my application needs to run for 24 hours, I know how inefficient it can be using System.string. :(( How can I replace System.string with StringBuilder? Like in System.Xml.XmlElement object, its inner member InnerText is itself a System.string object. Similarly in System.Xml.XmlNode object, its inner member InnerText is also a System.string object. So when too many classes of .net framework are using System.string type for their members (instead of StringBuilder), how can I replace System.string with StringBuilder? I am not getting any clue to come out of this problem. Even if I use StringBuilder, too many .net classes do not accept it. Using System.string or StringBuilder.ToString() can make my application really inefficient. Please help me to know how to go about it? Also let me know when System.string get collected by Garbage Collector? Thanks in Advance Aseem

    C OriginalGriffO R realJSOPR 4 Replies Last reply
    0
    • A Aseem Sharma

      Hi, In my application I am very much using the System.string class for every manipulation of characters (Reading a lot of data from a XML file, writing a lot of data in a XML file and creating log for each operation). If my application needs to run for 24 hours, I know how inefficient it can be using System.string. :(( How can I replace System.string with StringBuilder? Like in System.Xml.XmlElement object, its inner member InnerText is itself a System.string object. Similarly in System.Xml.XmlNode object, its inner member InnerText is also a System.string object. So when too many classes of .net framework are using System.string type for their members (instead of StringBuilder), how can I replace System.string with StringBuilder? I am not getting any clue to come out of this problem. Even if I use StringBuilder, too many .net classes do not accept it. Using System.string or StringBuilder.ToString() can make my application really inefficient. Please help me to know how to go about it? Also let me know when System.string get collected by Garbage Collector? Thanks in Advance Aseem

      C Offline
      C Offline
      Chetan Patel
      wrote on last edited by
      #2

      Hi, StringBuilder is Benificial in case of string manipulations like concat string for no of times. Then use StringBuilder and add method to create result string. But when you got final string then You can use String object.

      Best Regards, Chetan Patel

      A 1 Reply Last reply
      0
      • A Aseem Sharma

        Hi, In my application I am very much using the System.string class for every manipulation of characters (Reading a lot of data from a XML file, writing a lot of data in a XML file and creating log for each operation). If my application needs to run for 24 hours, I know how inefficient it can be using System.string. :(( How can I replace System.string with StringBuilder? Like in System.Xml.XmlElement object, its inner member InnerText is itself a System.string object. Similarly in System.Xml.XmlNode object, its inner member InnerText is also a System.string object. So when too many classes of .net framework are using System.string type for their members (instead of StringBuilder), how can I replace System.string with StringBuilder? I am not getting any clue to come out of this problem. Even if I use StringBuilder, too many .net classes do not accept it. Using System.string or StringBuilder.ToString() can make my application really inefficient. Please help me to know how to go about it? Also let me know when System.string get collected by Garbage Collector? Thanks in Advance Aseem

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        StringBuilder is only going to be more efficient than a string if: 1) It never gets extended - each time it does, it gets re-allocated, just like a string. 2) If your code repeatedly changes the string content. If you are allocating strings and keeping them allocated, then replacing them with StringBuilder (which you can't do for System.Xml objects) will not make any real improvement unless you overwrite the StringBuilder content. How are you allocating / using the strings? If you app is running for 24 hours, what happens? Strings are collected by the GC when it feels like it (i.e. when it needs to make some space for a new allocation), but will only be collected when there are no further references to them.

        If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        A 1 Reply Last reply
        0
        • A Aseem Sharma

          Hi, In my application I am very much using the System.string class for every manipulation of characters (Reading a lot of data from a XML file, writing a lot of data in a XML file and creating log for each operation). If my application needs to run for 24 hours, I know how inefficient it can be using System.string. :(( How can I replace System.string with StringBuilder? Like in System.Xml.XmlElement object, its inner member InnerText is itself a System.string object. Similarly in System.Xml.XmlNode object, its inner member InnerText is also a System.string object. So when too many classes of .net framework are using System.string type for their members (instead of StringBuilder), how can I replace System.string with StringBuilder? I am not getting any clue to come out of this problem. Even if I use StringBuilder, too many .net classes do not accept it. Using System.string or StringBuilder.ToString() can make my application really inefficient. Please help me to know how to go about it? Also let me know when System.string get collected by Garbage Collector? Thanks in Advance Aseem

          R Offline
          R Offline
          Rob Philpott
          wrote on last edited by
          #4

          Well, StringBuilder is used when you want to build up a big string by concatinating small bits together. It's used as strings are immutable, that is they can't be changed. So, if you add two strings together, a third is created and the contents of both are copied over. Obviously this causes problems if you do it repeatedly, and StringBuilder exists to mitigate this performance problem. In terms of GC, I believe that strings are collected in the normal manner like any other object, that is at some undeterminate point after all references to it no longer exist.

          Regards, Rob Philpott.

          1 Reply Last reply
          0
          • A Aseem Sharma

            Hi, In my application I am very much using the System.string class for every manipulation of characters (Reading a lot of data from a XML file, writing a lot of data in a XML file and creating log for each operation). If my application needs to run for 24 hours, I know how inefficient it can be using System.string. :(( How can I replace System.string with StringBuilder? Like in System.Xml.XmlElement object, its inner member InnerText is itself a System.string object. Similarly in System.Xml.XmlNode object, its inner member InnerText is also a System.string object. So when too many classes of .net framework are using System.string type for their members (instead of StringBuilder), how can I replace System.string with StringBuilder? I am not getting any clue to come out of this problem. Even if I use StringBuilder, too many .net classes do not accept it. Using System.string or StringBuilder.ToString() can make my application really inefficient. Please help me to know how to go about it? Also let me know when System.string get collected by Garbage Collector? Thanks in Advance Aseem

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            You can't change the return values of framework object methods (and indeed, you don't want to) . In your own object methods, you can use StringBuilder internally if necessary, but unless you have a very specific reason for doing so, there's really no reason to return a StringBuilder object. A StringBuilder is a more efficient object when frequent changes to a given string are made, but it saves you nothing as a return value unless the method being returned to is also going to heavily modify the returned value. If you're concerned with memory usage, buy a memory profiler, and run your app for a few days to see if this is indeed something that even needs to be considered. You may find that your efforts are best concentrated elsewhere in the code.

            .45 ACP - because shooting twice is just silly
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

            1 Reply Last reply
            0
            • C Chetan Patel

              Hi, StringBuilder is Benificial in case of string manipulations like concat string for no of times. Then use StringBuilder and add method to create result string. But when you got final string then You can use String object.

              Best Regards, Chetan Patel

              A Offline
              A Offline
              Aseem Sharma
              wrote on last edited by
              #6

              Thanks

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                StringBuilder is only going to be more efficient than a string if: 1) It never gets extended - each time it does, it gets re-allocated, just like a string. 2) If your code repeatedly changes the string content. If you are allocating strings and keeping them allocated, then replacing them with StringBuilder (which you can't do for System.Xml objects) will not make any real improvement unless you overwrite the StringBuilder content. How are you allocating / using the strings? If you app is running for 24 hours, what happens? Strings are collected by the GC when it feels like it (i.e. when it needs to make some space for a new allocation), but will only be collected when there are no further references to them.

                If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

                A Offline
                A Offline
                Aseem Sharma
                wrote on last edited by
                #7

                Earlier I was under a wrong concept that System.Strings are not collected by GC. Thanks for correcting me.

                OriginalGriffO 1 Reply Last reply
                0
                • A Aseem Sharma

                  Earlier I was under a wrong concept that System.Strings are not collected by GC. Thanks for correcting me.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  You are welcome! Everything is subject to the GC as soon as it's last reference is removed.

                  If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups