Thursday 1 July 2010

TIP: Improve Readability with Shorter String.Format Statements

String.Format is a wonderful method, a real life saver when it comes to producing (readable) formatted text from within code.  I use it everywhere, but it gets a bit tedious typing the same boilerplate code to use it properly:

   1: string formatted = string.Format(CultureInfo.InvariantCulture, "Formatted text {0:-15} example generated on {1:d}", meaningfulString, DateTime.Now);

That “string.Format(CultureInfo.InvariantCulture,” over 40 characters before you get the meat of the statement.  Sure you can drop the invariant culture bit but then you can introduce weird formatting problems on different machines….no what I need is a useful extension method to take my pain away:

   1: /// <summary>
   2: /// Populates the template using the provided arguments and the invariante culture
   3: /// </summary>
   4: /// <param name="template">The template.</param>
   5: /// <param name="args">The args.</param>
   6: public static string ToFormattedString(this string template, params object[] args)
   7: {
   8:     return template.ToFormattedString(CultureInfo.InvariantCulture, args);
   9: }
  10:  
  11: /// <summary>
  12: /// Populates the template using the provided arguments usign the provided formatter
  13: /// </summary>
  14: /// <param name="template">The template.</param>
  15: /// <param name="formatter">The formatter.</param>
  16: /// <param name="args">The args.</param>
  17: public static string ToFormattedString(this string template, IFormatProvider formatter, params object[] args)
  18: {
  19:     if (string.IsNullOrEmpty(template)) return string.Empty;
  20:     return string.Format(formatter, template, args);
  21: }

Now the above example becomes:

   1: string formatted = "Formatted text {0:-15} example generated on {1:d}".ToFormattedString(meaningfulString, DateTime.Now);

It’s definitely an improvement and the important bit of the statement (the template with formatting) is right at the front for easy debugging.

Excellent, How Do I Retrofit This Into My Existing Code?

Good question, glad you asked.  I simply used Visual Studios Find and Replace using regular expressions:

image

The find regex (using VS’s “special” regex format) is:

string.Format\(CultureInfo.InvariantCulture,:b*{:q|:i}:b@,:b@

The replace regex is:

\1.ToFormattedString(

Obviously, you’ll also need a ‘using’' statement at the top of your class file with the namespace of the static class containing the extension methods.

No comments:

Post a Comment

Got something to say? Let it out then!
Comments are moderated, so it may take a while to for them to be displayed here!