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. Concating strings...

Concating strings...

Scheduled Pinned Locked Moved C#
performancequestiondiscussion
4 Posts 4 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.
  • S Offline
    S Offline
    Shy Agam
    wrote on last edited by
    #1

    Hello experts, It is well known that when a string's length is already known or can be easily calculated, it is best to use StringBuilder instead of string. But... How should one go about handling the following situation: string alreadyAllocated = "ABCD"; // Somewhere along the code... string newString = "BLABLA" + alreadyAllocated; The way I see it, it's the same as doing: string alreadyAllocated = "ABCD"; // Somewhere along the code...StringBuilder newString = new StringBuilder(alreadyAllocated.Length + 6); newString.Append("BLABLA"); newString.Append(alreadyAllocated); The reason I think it's the same is because to my understanding, when hardcoding the "BLABLA" string, memory is allocated for it anyway. So the resulting pseudo code in both cases would be something like: - Allocate memory for "BLABLA" - Allocate memory for "BLABLA" + alreadyAllocated - Copy both allocated strings to the new large location Care to share your knowledge and opinions? Thanks in advance, Shy.

    P G L 3 Replies Last reply
    0
    • S Shy Agam

      Hello experts, It is well known that when a string's length is already known or can be easily calculated, it is best to use StringBuilder instead of string. But... How should one go about handling the following situation: string alreadyAllocated = "ABCD"; // Somewhere along the code... string newString = "BLABLA" + alreadyAllocated; The way I see it, it's the same as doing: string alreadyAllocated = "ABCD"; // Somewhere along the code...StringBuilder newString = new StringBuilder(alreadyAllocated.Length + 6); newString.Append("BLABLA"); newString.Append(alreadyAllocated); The reason I think it's the same is because to my understanding, when hardcoding the "BLABLA" string, memory is allocated for it anyway. So the resulting pseudo code in both cases would be something like: - Allocate memory for "BLABLA" - Allocate memory for "BLABLA" + alreadyAllocated - Copy both allocated strings to the new large location Care to share your knowledge and opinions? Thanks in advance, Shy.

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

      Try

             System.Text.StringBuilder sb = new System.Text.StringBuilder ( "ABCD" ) ;
             sb.Insert ( 0 , "BLABLA" , 1 ) ;
      
      1 Reply Last reply
      0
      • S Shy Agam

        Hello experts, It is well known that when a string's length is already known or can be easily calculated, it is best to use StringBuilder instead of string. But... How should one go about handling the following situation: string alreadyAllocated = "ABCD"; // Somewhere along the code... string newString = "BLABLA" + alreadyAllocated; The way I see it, it's the same as doing: string alreadyAllocated = "ABCD"; // Somewhere along the code...StringBuilder newString = new StringBuilder(alreadyAllocated.Length + 6); newString.Append("BLABLA"); newString.Append(alreadyAllocated); The reason I think it's the same is because to my understanding, when hardcoding the "BLABLA" string, memory is allocated for it anyway. So the resulting pseudo code in both cases would be something like: - Allocate memory for "BLABLA" - Allocate memory for "BLABLA" + alreadyAllocated - Copy both allocated strings to the new large location Care to share your knowledge and opinions? Thanks in advance, Shy.

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

        shyagam wrote:

        It is well known that when a string's length is already known or can be easily calculated, it is best to use StringBuilder instead of string.

        Well known by whom? The StringBuilder is best used when you don't know the length of the final string.

        shyagam wrote:

        The reason I think it's the same is because to my understanding, when hardcoding the "BLABLA" string, memory is allocated for it anyway.

        Yes, if you use a StringBuilder to just concatenate two strings, then there is no difference in how the memory is allocated.

        --- single minded; short sighted; long gone;

        1 Reply Last reply
        0
        • S Shy Agam

          Hello experts, It is well known that when a string's length is already known or can be easily calculated, it is best to use StringBuilder instead of string. But... How should one go about handling the following situation: string alreadyAllocated = "ABCD"; // Somewhere along the code... string newString = "BLABLA" + alreadyAllocated; The way I see it, it's the same as doing: string alreadyAllocated = "ABCD"; // Somewhere along the code...StringBuilder newString = new StringBuilder(alreadyAllocated.Length + 6); newString.Append("BLABLA"); newString.Append(alreadyAllocated); The reason I think it's the same is because to my understanding, when hardcoding the "BLABLA" string, memory is allocated for it anyway. So the resulting pseudo code in both cases would be something like: - Allocate memory for "BLABLA" - Allocate memory for "BLABLA" + alreadyAllocated - Copy both allocated strings to the new large location Care to share your knowledge and opinions? Thanks in advance, Shy.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, StringBuilder is very useful and efficient when it allows you to avoid a lot of intermediate string objects. And it is best to create it with sufficient initial capacity. So instead of doing (silly example!):

          string s="";
          foreach (char c in "abcdefghijklmnopqrstuvwxyz") s=s+c;
          Console.WriteLine(s);

          which creates more than 20 string objects, you'd better do:

          StringBuilder sb=new StringBuilder(100); // large enough
          foreach (char c in "abcdefghijklmnopqrstuvwxyz") sb.Append(c);
          string s=sb.ToString();
          Console.WriteLine(s);

          which only creates one StringBuilder and one string. Since the capacity is sufficient, sb will never need an internal extend-and-copy (as in good old Basic Redim). StringBuilder will not give you a more efficient way to do a simple string3=string1+string2 tho. :)

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


          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