Results 1 to 5 of 5

Thread: Abuse of StringBuilder + String.Format - any better ways?

  1. #1
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,230
    Thanked
    2,291 times in 1,874 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Abuse of StringBuilder + String.Format - any better ways?

    Could use a little advice, or possibly just reassurance! I strongly suspect that I am murdering .NET (not that I really care about .NET, you understand ) and could do with some other opinions on whether what I'm doing is actually that bad, and whether I'm missing an obvious way to do it better.

    I'm in the middle of writing some code that requires a nice hefty dynamic string, built in numerous stages, each stage having a number of dynamic elements. The former element of this would suggest use of a StringBuilder, and the later would suggest use of String.Format. So, that's what I'm doing:
    Code:
    sb.Append(String.Format(.....)); // * many!
    But something about it just seems wrong (apart from the fact that I'm working with Microsoft technologies, which is something I still haven't quite gotten used to ). So, is my code a travesty, or am I just being paranoid?

  2. #2
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: Abuse of StringBuilder + String.Format - any better ways?

    As it sounds like you know strings in .Net are immutable, that is to say they can not be changed, so:

    string foo = "b" + "a" + "a" + "!" will actually compile into these objects been created
    b
    ba
    baa
    baa!

    By using stringbuilder you have a stream of data, if your using String.Format a new string is been created temporarily just be added to the builder.

    This isn't ideal if you call that line many times.

    http://msdn.microsoft.com/en-us/libr...endformat.aspx

    By doing
    Code:
    for (int i = 0; i < 10000; i++)
    sb.AppendFormat("{0} other string {1}",s1[i],s2[i]);
    it will make fewer string objects!
    throw new ArgumentException (String, String, Exception)

  3. Received thanks from:

    scaryjim (25-02-2010)

  4. #3
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,230
    Thanked
    2,291 times in 1,874 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: Abuse of StringBuilder + String.Format - any better ways?

    Ooooh, AppendFormat? You know, it didn't even occur to me to look for a StringBuilder method that would do it all in one, but I kind of instinctively felt there should be something like that!

    That's exactly what I was looking for - cheers


    p.s. I should say that in this particular instance it probably won't make that much difference because it's a maximum of about 20 calls and it's a low use section of the application, but since I'm likely to do something similar again in the future when it might make a difference it's good to know what I *should* be doing

  5. #4
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: Abuse of StringBuilder + String.Format - any better ways?

    yeh another 'top tip' for .net converts (obvious probably in your case because i'm guessing your from java) is as said above to avoid

    string s = "hello" + " " + "world" + variable + ".";

    that creates an object for each + operator (as the contract comes from string OP_Addition(string s1, string s2)).

    instead string.Concact("many","variables","concatenated","in","one","op");
    throw new ArgumentException (String, String, Exception)

  6. #5
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,230
    Thanked
    2,291 times in 1,874 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: Abuse of StringBuilder + String.Format - any better ways?

    Well, I learned Java for my MSc, but I converted to .NET from classic ASP / VBScript (which I moved into from VBA, which I taught myself alongside Perl, both of which I picked up after having self-learned javascript, all way back in the day. My code can get a bit hackish occasionally ). Of course, that means my .NET is entirely self taught, and all my projects are on the kind of deadlines where I tend to have time to find out *how* not to do something, but seldom *why* not to do it!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Reputation System - Abuse of .... *** WARNING ***
    By Saracen in forum General Discussion
    Replies: 15
    Last Post: 20-08-2004, 04:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •