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. Other Discussions
  3. Clever Code
  4. Building strings

Building strings

Scheduled Pinned Locked Moved Clever Code
csharp
30 Posts 17 Posters 1 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.
  • V V 0

    I was a bit sceptic about the StringBuilder class of .NET and always used + and += to concatenate strings. Know I saw with my own two eyes the difference between the two... :omg:. For creating about 2MB of text and writing to a file it was a difference of MINUTES ! (It was a complex method with recursive calling etc...) (Well, better find out late then never...) (I was doing this with my old method and was surprised that it was so slow... for the 'fun' of it I tried the StringBuilder) So if your app is slow and you're using the + and += way... this is your solution...


    V. Stop smoking so you can: enjoy longer the money you save.

    A Offline
    A Offline
    Alois Kraus
    wrote on last edited by
    #21

    If you have to alter very big strings StringBuilder is the way to go. But for small strings the border where StringBuilder is faster than the String.xxx functions becomes a moving target: http://www.codeproject.com/useritems/StringBuilder_vs_String.asp Yours, Alois Kraus

    1 Reply Last reply
    0
    • V V 0

      I was a bit sceptic about the StringBuilder class of .NET and always used + and += to concatenate strings. Know I saw with my own two eyes the difference between the two... :omg:. For creating about 2MB of text and writing to a file it was a difference of MINUTES ! (It was a complex method with recursive calling etc...) (Well, better find out late then never...) (I was doing this with my old method and was surprised that it was so slow... for the 'fun' of it I tried the StringBuilder) So if your app is slow and you're using the + and += way... this is your solution...


      V. Stop smoking so you can: enjoy longer the money you save.

      J Offline
      J Offline
      Jorgen Sigvardsson
      wrote on last edited by
      #22

      The reason why this is the case is that strings in .NET (just like in Java), are immutable objects. That means that the underlying memory buffer is locked, and will not allow modifications. String s = "ab"; s += "c"; is equivalent to String s = "ab"; s = s + "c" which in turn is equivalent to String s = "ab"; String temp = s + "c"; s = temp;. Each append generates a new object. StringBuilder however, is a mutable object, meaning that the underlying character array is modified when you issue an append command - no temporary objects are created. The reason why strings are immutable is that you can do a fair amount of optimizations on strings, which will improve performance - given that you do not misuse them of course. :)

      -- Presented in doublevision (where drunk)

      1 Reply Last reply
      0
      • V V 0

        I was a bit sceptic about the StringBuilder class of .NET and always used + and += to concatenate strings. Know I saw with my own two eyes the difference between the two... :omg:. For creating about 2MB of text and writing to a file it was a difference of MINUTES ! (It was a complex method with recursive calling etc...) (Well, better find out late then never...) (I was doing this with my old method and was surprised that it was so slow... for the 'fun' of it I tried the StringBuilder) So if your app is slow and you're using the + and += way... this is your solution...


        V. Stop smoking so you can: enjoy longer the money you save.

        P Offline
        P Offline
        Pascal Ganaye
        wrote on last edited by
        #23

        I think I read somewhere that StringBuilder is not a class that you could write in managed C# or VB. It is managed C# but It uses string internals which are not available to us. This result in a much better memory management. I had a look at the source, and I believe it is an awfully long winded. Anyway It is MUCH better than using +. This said I have seen people using StringBuilder to concatenate 2 strings and show the result in a message box. I think it is a bit over the top, stringbuilder beat the + operator when .append is used repeatidly on the same stringbuilder. What the point of saving 0.1 millisecond to display a messagebox. Overall in the long run code clarity is as important as speed, I would recommend to use stringbuilder when you concatenate 3 items or more.

        1 Reply Last reply
        0
        • L Lost User

          StringBuilder was added to Java 1.5. :) http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html[^]


          Kicking squealing Gucci little piggy.
          The Rob Blog

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #24

          So they have the thread safe StringBuffer and the non-thread safe StringBuilder, both with identical functionality. Yeah, that's not confusing. :rolleyes:

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          D 1 Reply Last reply
          0
          • M Matt Gerrans

            That's funny. I guess its been a while since I've used Java. ;) I guess Microsoft and Sun borrow class names from each other now.

            Matt Gerrans

            G Offline
            G Offline
            GaryWoodfine
            wrote on last edited by
            #25

            MS probably copied it from Java :-)


            "a fool will not learn from a wise man, but a wise man will learn from a fool" "It is hard to fail, but it is worse never to have tried to succeed." - Theodore Roosevelt "Success is going from failure to failure without losing your enthusiasm." - Winston Churchill


            My Website || My Blog

            Z 1 Reply Last reply
            0
            • V V 0

              LOL, but this is a microsoft site ;-). Java is against the rules ;p

              V.
              Stop smoking so you can: Enjoy longer the money you save. Moviereview Archive

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #26

              V. wrote:

              but this is a microsoft site

              *looks around* I don't recall seeing anywhere that stated this site ONLY dealt with Microsoft products. In fact, I've seen several questions/comments on here dealing with compiling code with Intel C++, GCC, and Borland ... and even some using Linux ;P

              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              N 1 Reply Last reply
              0
              • G GaryWoodfine

                MS probably copied it from Java :-)


                "a fool will not learn from a wise man, but a wise man will learn from a fool" "It is hard to fail, but it is worse never to have tried to succeed." - Theodore Roosevelt "Success is going from failure to failure without losing your enthusiasm." - Winston Churchill


                My Website || My Blog

                Z Offline
                Z Offline
                Zac Howland
                wrote on last edited by
                #27

                cykophysh39 wrote:

                MS probably copied it from Java

                Not likely. StringBuilder was part of .Net long before Java 1.5 was released. Sun and Microsoft are going back and forth with their library support, so don't be surprised when you see similar classes doing similar things coming from both of them. When one does it, the other will soon copy it.

                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                1 Reply Last reply
                0
                • J Judah Gabriel Himango

                  So they have the thread safe StringBuffer and the non-thread safe StringBuilder, both with identical functionality. Yeah, that's not confusing. :rolleyes:

                  Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                  D Offline
                  D Offline
                  David Stone
                  wrote on last edited by
                  #28

                  There's a lot of stuff like that in Java. ArrayList<T> and Vector<T> are interchangeable. One of them is thread safe. Go figure.

                  Once you wanted revolution
                  Now you're the institution
                  How's it feel to be the man?

                  J 1 Reply Last reply
                  0
                  • D David Stone

                    There's a lot of stuff like that in Java. ArrayList<T> and Vector<T> are interchangeable. One of them is thread safe. Go figure.

                    Once you wanted revolution
                    Now you're the institution
                    How's it feel to be the man?

                    J Offline
                    J Offline
                    Judah Gabriel Himango
                    wrote on last edited by
                    #29

                    David Stone wrote:

                    There's a lot of stuff like that in Java. ArrayList and Vector are interchangeable. One of them is thread safe. Go figure.

                    :doh:

                    Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                    1 Reply Last reply
                    0
                    • Z Zac Howland

                      V. wrote:

                      but this is a microsoft site

                      *looks around* I don't recall seeing anywhere that stated this site ONLY dealt with Microsoft products. In fact, I've seen several questions/comments on here dealing with compiling code with Intel C++, GCC, and Borland ... and even some using Linux ;P

                      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                      N Offline
                      N Offline
                      Nitron
                      wrote on last edited by
                      #30

                      Zac Howland wrote:

                      and even some using Linux

                      soapbox! :-D

                      ~Nitron.


                      ññòòïðïðB A
                      start

                      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