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

A recent question has arisen due to the use of String.Format (). Part of my answer included a suggestion to use StringBuilder.AppendLine (string.Format (...)). John Skeet suggested this was a bad example, and suggested using a combination of AppendLine and AppendFormat.

It occurred to me that I never settled in a “preferred” approach to using these methods. I think I could start using something like the following, but I am interested to know what other people use as “best practice”:

sbuilder.AppendFormat("{0} line", "First").AppendLine(); sbuilder.AppendFormat("{0} line", "Second").AppendLine(); // as opposed to: sbuilder.AppendLine( String.Format( "{0} line", "First")); sbuilder.AppendLine( String.Format( "{0} line", "Second")); 
+49
stringbuilder c # formatting
Dec 08 '08 at 14:32
source share
7 answers

I consider AppendFormat and then AppendLine as not only more readable, but also more efficient than calling AppendLine(string.Format(...)) .

The latter creates a whole new line and then adds it to an existing builder. I'm not going to say this: "Why then use StringBuilder?" but it is a bit like the spirit of StringBuilder.

+55
Dec 08 '08 at 14:37
source share

String.format creates a StringBuilder object inside. Performing

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

An additional instance of the string constructor with all its auxiliary data.




Reflector on mscorlib, Commonlauageruntelelibary, 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(); } 
+11
Dec 08 '08 at 18:55
source share

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:

  • Not as much overhead as AppendLine(string.Format(...)) as mentioned above.
  • It prevents that I forgot to add the .AppendLine() part to the end (this happens quite often).
  • More readable (but rather an opinion).

If you don’t like the fact that it is called "AppendLine", you can change it to "AppendFormattedLine" or whatever you want. I like everything related to other calls in AppendLine:

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

Just add one of them for each overload used by the AppendFormat method in StringBuilder.

+11
Sep 10 '13 at 21:41
source share

If performance is important, try to completely abandon AppendFormat (). Instead, use multiple calls to Append () or AppendLine (). This makes your code larger and less readable, but it is faster because you do not need to parse strings. Parsing strings is slower than you could imagine.

I usually use:

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

If performance is not critical, then I would use:

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

(Of course, this makes sense if "First" and "Second", where there are no string literals)

+2
Dec 08 '08 at 17:46
source share

AppendFormat () is much more readable than AppendLine (String.Format ())

0
Dec 08 '08 at 14:37
source share

I prefer this structure:

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

Although, admittedly, there is something to be said to separate line breaks.

0
Dec 08 '08 at 14:49
source share

Is it really just awful to just use

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

? I mean, I know that it does not depend on the platform or something else, but in 9 out of 10 cases it does its job.

0
Dec 08 '08 at 14:53
source share



All Articles