Click here to Skip to main content
15,867,704 members
Articles / Multimedia / GDI

TIP: Improve Readability with Shorter String.Format Statements

Rate me:
Please Sign up or sign in to vote.
3.75/5 (6 votes)
3 Jul 2010CPOL 13.4K   3   4
How to 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:

C#
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:

C#
/// <summary>
/// Populates the template using the provided arguments and the invariant culture
/// </summary>
/// <param name="template">The template.
/// <param name="args">The args.
public static string ToFormattedString(this string template, params object[] args)
{
    return template.ToFormattedString(CultureInfo.InvariantCulture, args);
}

/// <summary>
/// Populates the template using the provided arguments using the provided formatter
/// </summary>
/// <param name="template">The template.
/// <param name="formatter">The formatter.
/// <param name="args">The args.
public static string ToFormattedString(this string template,
   IFormatProvider formatter, params object[] args)
{
    if (string.IsNullOrEmpty(template)) return string.Empty;
    return string.Format(formatter, template, args);
}

Now the above example becomes:

C#
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 Studio's 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Freestyle Interactive Ltd
United Kingdom United Kingdom
I'm a lead developer for Freestyle Interactive Ltd where we create many wonderful websites built on Microsofts ASP.Net and Ektron CMS.

I've been developing .Net applications (both Windows and Web) since 2002.

Comments and Discussions

 
QuestionUse Interpolated strings Pin
Chris Maunder4-Aug-17 6:21
cofounderChris Maunder4-Aug-17 6:21 
GeneralMy vote of 1 Pin
Eugene Sichkar15-Jul-10 23:56
Eugene Sichkar15-Jul-10 23:56 
GeneralRe: My vote of 1 Pin
Martin Jarvis3-Aug-10 11:17
Martin Jarvis3-Aug-10 11:17 
Whilst this isn't the most feature rich example of an extension method, it does save a considerable number of keystrokes on each use - particularly if you need to write global applications.

As I code in a team (and we make use of many contractors/outsource companies) there is a wide range of abilities and not every developer understands the importance of properly localized code. Offering a quicker alternative is a vital step to encourage compliance.

GeneralMy vote of 5 Pin
Darchangel6-Jul-10 9:50
Darchangel6-Jul-10 9:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.