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. Stringbuilder

Stringbuilder

Scheduled Pinned Locked Moved C#
questionlearning
9 Posts 6 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.
  • J Offline
    J Offline
    jeramyRR
    wrote on last edited by
    #1

    When would I use stringbuilder over just string? I just read about it in a book but it doesn't explain why you would use it.

    E V A K 4 Replies Last reply
    0
    • J jeramyRR

      When would I use stringbuilder over just string? I just read about it in a book but it doesn't explain why you would use it.

      E Offline
      E Offline
      egenis
      wrote on last edited by
      #2

      I would say when you are concatenating a number of strings use a stringbuilder. If it's just a case of concatenating first name and surname of a person object, a stringbuilder would be an overkill.

      1 Reply Last reply
      0
      • J jeramyRR

        When would I use stringbuilder over just string? I just read about it in a book but it doesn't explain why you would use it.

        V Offline
        V Offline
        VJ Reddy
        wrote on last edited by
        #3

        In .NET, string class is immutable. Simply put, it means that you cannot modify a string after it's creation. So, for example

        string someString = "Hello";
        //Now if we write
        someString += " World";

        The program will not add World to the existing string. Instead, it will create a new string object "Hello World" and assigns this to someString and discards the earlier object "Hello". So, this may not be a big issue if we are dealing with only a few strings. But, if we want to deal with several string operations, then StringBuilder is a good option. As explained here http://msdn.microsoft.com/en-us/library/y9sxk6fy.aspx[^] This class represents a string-like object whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters. And after making all the modifications ToString method of StringBuilder can be used to get the final string. I think this Code Project article StringBuilder vs. String, Fast String Operations with .NET 2.0[^] may be helpful.

        J 1 Reply Last reply
        0
        • J jeramyRR

          When would I use stringbuilder over just string? I just read about it in a book but it doesn't explain why you would use it.

          A Offline
          A Offline
          Abhinav S
          wrote on last edited by
          #4

          This discussion[^] could be useful to you.

          V L 2 Replies Last reply
          0
          • A Abhinav S

            This discussion[^] could be useful to you.

            V Offline
            V Offline
            VJ Reddy
            wrote on last edited by
            #5

            Good link. 5!

            1 Reply Last reply
            0
            • A Abhinav S

              This discussion[^] could be useful to you.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Nice one By Abhinav 5+ thanks for info.

              1 Reply Last reply
              0
              • V VJ Reddy

                In .NET, string class is immutable. Simply put, it means that you cannot modify a string after it's creation. So, for example

                string someString = "Hello";
                //Now if we write
                someString += " World";

                The program will not add World to the existing string. Instead, it will create a new string object "Hello World" and assigns this to someString and discards the earlier object "Hello". So, this may not be a big issue if we are dealing with only a few strings. But, if we want to deal with several string operations, then StringBuilder is a good option. As explained here http://msdn.microsoft.com/en-us/library/y9sxk6fy.aspx[^] This class represents a string-like object whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters. And after making all the modifications ToString method of StringBuilder can be used to get the final string. I think this Code Project article StringBuilder vs. String, Fast String Operations with .NET 2.0[^] may be helpful.

                J Offline
                J Offline
                jeramyRR
                wrote on last edited by
                #7

                Thank you. After reading that it kind of reminds me of how I used buffers in C.

                1 Reply Last reply
                0
                • J jeramyRR

                  When would I use stringbuilder over just string? I just read about it in a book but it doesn't explain why you would use it.

                  K Offline
                  K Offline
                  Keith Barrow
                  wrote on last edited by
                  #8

                  John Skeet has written about this[^] This is an excellent introduction to this topic. I once scored brownie points in a Job Interview for referring to this stuff when asked why I didn't use StringBuilder in a simple one-time concatenation during a programming test. The interviewer didn't beleive me at first, but a quick test showed similar results to the article.

                  Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                  -Or-
                  A Dead ringer for Kate Winslett[^]

                  V 1 Reply Last reply
                  0
                  • K Keith Barrow

                    John Skeet has written about this[^] This is an excellent introduction to this topic. I once scored brownie points in a Job Interview for referring to this stuff when asked why I didn't use StringBuilder in a simple one-time concatenation during a programming test. The interviewer didn't beleive me at first, but a quick test showed similar results to the article.

                    Sort of a cross between Lawrence of Arabia and Dilbert.[^]
                    -Or-
                    A Dead ringer for Kate Winslett[^]

                    V Offline
                    V Offline
                    VJ Reddy
                    wrote on last edited by
                    #9

                    Good link. 5!

                    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