When do you use StringBuilder.AppendLine/string.Format vs. StringBuilder.AppendFormat?

前端 未结 7 710
半阙折子戏
半阙折子戏 2021-01-31 13:34

A recent question came up about using String.Format(). Part of my answer included a suggestion to use StringBuilder.AppendLine(string.Format(...)). Jon Skeet suggested this was

相关标签:
7条回答
  • 2021-01-31 14:12

    String.format creates a StringBuilder object internally. By doing

    sbuilder.AppendLine( String.Format( "{0} line", "First"));
    

    an additional instance of string builder, with all of its overhead is created.


    Reflector on mscorlib, Commonlauageruntimelibary, System.String.Format

    public static string Format(IFormatProvider provider, string format, params object[] args)
    {
        if ((format == null) || (args == null))
        {
            throw new ArgumentNullException((format == null) ? "format" : "args");
        }
        StringBuilder builder = new StringBuilder(format.Length + (args.Length * 8));
        builder.AppendFormat(provider, format, args);
        return builder.ToString();
    }
    
    0 讨论(0)
  • 2021-01-31 14:12

    I prefer this structure:

    sbuilder.AppendFormat("{0} line\n", "First");
    

    Though admittedly there is something to be said for separating out the line breaks.

    0 讨论(0)
  • 2021-01-31 14:15

    Just create an extension method.

    public static StringBuilder AppendLine(this StringBuilder builder, string format, params object[] args)
    {
        builder.AppendFormat(format, args).AppendLine();
        return builder;
    }
    

    Reasons I prefer this:

    • Doesn't suffer as much overhead as AppendLine(string.Format(...)), as stated above.
    • Prevents me from forgetting to add the .AppendLine() part at the end (happens frequently enough).
    • Is more readable (but that is more of an opinion).

    If you don't like it being called 'AppendLine,' you could change it to 'AppendFormattedLine' or whatever you want. I enjoy everything lining up with other calls to 'AppendLine' though:

    var builder = new StringBuilder();
    
    builder
        .AppendLine("This is a test.")
        .AppendLine("This is a {0}.", "test");
    

    Just add one of these for each overload you use of the AppendFormat method on StringBuilder.

    0 讨论(0)
  • 2021-01-31 14:21

    Is it just positively awful to simply use

    sbuilder.AppendFormat("{0} line\n", first);
    

    ? I mean, I know it's not platform-independent or whatever, but in 9 out of 10 cases it gets the job done.

    0 讨论(0)
  • 2021-01-31 14:22

    If performance is important, try to avoid AppendFormat() completely. Use multiple Append() or AppendLine() calls instead. This does make your code larger and less readable, but it's faster because no string parsing has to be done. String parsing is slower than you might imagine.

    I generally use:

    sbuilder.AppendFormat("{0} line", "First");
    sbuilder.AppendLine();
    sbuilder.AppendFormat("{0} line", "Second");
    sbuilder.AppendLine();
    

    Unless performance is critical, in which case I'd use:

    sbuilder.Append("First");
    sbuilder.AppendLine(" line");
    sbuilder.Append("Second");
    sbuilder.AppendLine(" line");
    

    (Of course, this would make more sense if "First" and "Second" where not string literals)

    0 讨论(0)
  • 2021-01-31 14:31

    AppendFormat() is a lot more readable than AppendLine(String.Format())

    0 讨论(0)
提交回复
热议问题