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. String Concatenation

String Concatenation

Scheduled Pinned Locked Moved C#
questionperformance
12 Posts 9 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.
  • C Charith Jayasundara

    What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!

    Charith Jayasundara

    R Offline
    R Offline
    rah_sin
    wrote on last edited by
    #3

    u can use stringbuffer also

    rahul

    G 1 Reply Last reply
    0
    • C Charith Jayasundara

      What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!

      Charith Jayasundara

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #4

      I'd go with string.Concat.

      string key = string.Concat("test", "::", "test");

      Navaneeth How to use google | Ask smart questions

      1 Reply Last reply
      0
      • C Charith Jayasundara

        What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!

        Charith Jayasundara

        N Offline
        N Offline
        nelsonpaixao
        wrote on last edited by
        #5

        try STRINGBUILDER function for that

        nelsonpaixao@yahoo.com.br trying to help & get help

        1 Reply Last reply
        0
        • C Charith Jayasundara

          What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!

          Charith Jayasundara

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #6

          If that's all you're doing, I'd stick with method 1. (And let the compiler handle.) I often use method 2 when concatenating literal text with variables; I find it easier to read (when a lot of space is added). But if you're doing a bunch of concatenations in a loop (to produce one value), use StringBuilder. Also look into String.Join

          1 Reply Last reply
          0
          • C Charith Jayasundara

            What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!

            Charith Jayasundara

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

            I believe the 2nd example uses a StringBuilder internally, and for large strings, with multiple instances of addition, it's more efficient. I almost always use string.Format over "string + string".

            "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." - Jason Jystad, 10/26/2001

            G 1 Reply Last reply
            0
            • C Charith Jayasundara

              What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!

              Charith Jayasundara

              RaviBeeR Offline
              RaviBeeR Offline
              RaviBee
              wrote on last edited by
              #8

              Before you decide on a method to perform the concatenation, see this[^] link. /ravi

              My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

              1 Reply Last reply
              0
              • R rah_sin

                u can use stringbuffer also

                rahul

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #9

                StringBuffer? Isn't that a Java class? This is the C# forum...

                Despite everything, the person most likely to be fooling you next is yourself.

                1 Reply Last reply
                0
                • C Charith Jayasundara

                  What is the best way to contact three strings. Method1 string key = "test" +"::"+ "test"; Method2 sting key = String.Format("{0}::{1}","test","test"); Is there any memory enhancement in using the second method? Thanks!

                  Charith Jayasundara

                  G Offline
                  G Offline
                  Guffa
                  wrote on last edited by
                  #10

                  Your method 1 is absolutely the most efficient. It concatenates the strings while compiling, so the only work done at runtime is allocating a reference of an already existing string to the variable. I suppose that you meant to concatenate string values from variables, not literal strings, so there is actually something done at runtime: Method 1: string key = str1 + "::" + str2; Method 2: string key = string.Format("{0}::{1}", str1, str2); Method 1 is still the most efficient. It will compile into a call to the String.Concat(string, string, string) method, which adds the lengths of the strings, allocates a string with that size, and copies the data from the strings into the new string. Method 2 uses a StringBuilder, which is overkill when you just want to concatenate three strings. It calls the AppendFormat method which has to parse the format string to know what to do with the strings. Using the String.Format method often produces more readable code, and it can certainly do a lot more than just concatenating strings, but for your specific question it's not the most efficient.

                  Despite everything, the person most likely to be fooling you next is yourself.

                  modified on Thursday, January 1, 2009 9:04 AM

                  1 Reply Last reply
                  0
                  • realJSOPR realJSOP

                    I believe the 2nd example uses a StringBuilder internally, and for large strings, with multiple instances of addition, it's more efficient. I almost always use string.Format over "string + string".

                    "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." - Jason Jystad, 10/26/2001

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #11

                    John Simmons / outlaw programmer wrote:

                    with multiple instances of addition, it's more efficient

                    That's true, but that was not what the OP asked for. The question was regardning threee strings, and then the second method is not more efficient.

                    Despite everything, the person most likely to be fooling you next is yourself.

                    realJSOPR 1 Reply Last reply
                    0
                    • G Guffa

                      John Simmons / outlaw programmer wrote:

                      with multiple instances of addition, it's more efficient

                      That's true, but that was not what the OP asked for. The question was regardning threee strings, and then the second method is not more efficient.

                      Despite everything, the person most likely to be fooling you next is yourself.

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

                      Guffa wrote:

                      John Simmons / outlaw programmer wrote: with multiple instances of addition, it's more efficient That's true, but that was not what the OP asked for. The question was regardning threee strings, and then the second method is not more efficient.

                      And I told him *when* using string.Format is more efficient.

                      "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." - Jason Jystad, 10/26/2001

                      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