Get The Most Out Of Your Format String

0 comments suggest edit

I was reviewing some code when I ran into code that fit this pattern all over the place (simplified to make a point).

string format = “/comments/{0}/{1}.aspx#”;

string url = String.Format(CultureInfo.InvariantCulture, format + entity.Id, entity.ParentID, entity.Date.ToString(“yyyy/MM/dd”, CultureInfo.InvariantCulture));

There are a couple things I want you to notice here. The first is concatenation of the entity id to the end of the format string.

format + entity.Id

Second, notice format string passed into the ToString method of the the entity.Date.

entity.Date.ToString(“yyyy/MM/dd”, CultureInfo.InvariantCulture)

Ok I lied. There is a third thing I want you to notice - the fact that CultureInfo.InvariantCulture has to be specified twice.

My guess is the author of this code was in a hurry and didn’t realize the full power of format strings that can be passed to the String.Format method. It is possible to get all the formatting information in a single string. Here is how I rewrote this.

string format = “/comments/{0:yyyy/MM/dd}/{1}.aspx#{2}”;

string url = String.Format(CultureInfo.InvariantCulture, format, entity.Date, entity.ParentId, entity.Id);

In this rewritten code, I took advantage of the fact that the String.Format method allows you to specify formatting information within the place holders like so.

{0:yyyy/MM/dd}

In this way, the code does not need to call the ToString method of the DateTime instance. The formatting is all in a single format string and that is one less CultureInfo.InvariantCulture you need to type.

This helps make the code more readable and more flexible. The string format for this url is all in a single string.

A useful reference for string formats that I keep at my fingertips is the following blog post by Steve Tibbett. If you use format strings, you will find this reference invaluable.

Found a typo or error? Suggest an edit! If accepted, your contribution is listed automatically here.

Comments

avatar

3 responses

  1. Avatar for Ryan Farley
    Ryan Farley February 15th, 2006

    Phil,



    That post from Steve Tibbett is a great one for this topic. Also, Chris Sells posted a tool a couple years ago that comes in handy when you're trying out different format strings.



    http://www.sellsbrothers.com/tools/#FormatDesigner



    This lets you try out different formats and see the results right away.



    -Ryan

  2. Avatar for Haacked
    Haacked February 15th, 2006

    Ah! Thanks Ryan, That's pretty sweet.

  3. Avatar for Jon Galloway
    Jon Galloway February 15th, 2006

    Barry Dorrans has a nice overview of format strings, including sample formats. He also includes some lesser known features like alignment and group separators.



    http://idunno.org/displayBlog.aspx/2004071401