Get The Most Out Of Your Format String

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.

What others have said

Requesting Gravatar... Ryan Farley Feb 15, 2006 11:31 AM
# re: Get The Most Out Of Your Format String
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
Requesting Gravatar... Haacked Feb 15, 2006 11:58 AM
# re: Get The Most Out Of Your Format String
Ah! Thanks Ryan, That's pretty sweet.
Requesting Gravatar... Jon Galloway Feb 15, 2006 1:58 PM
# re: Get The Most Out Of Your Format String
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

What do you have to say?

(will show your gravatar)
Please add 7 and 4 and type the answer here: