.NET: String.Format drama

I believe the String.Format function is not a stranger to .NET developers. A lot of us used it as a cleaner solution to do string concatenation:

string name = "Leon";

// Doing this:
string s = "My name is: " + name + ".";
// is the same as:
string s1 = String.Format("My name is: {0}.", name);

It provides a nice and simple parameterized solution to build up strings without using StringBuilder. However, while String.Format is good for readability in concatenation, it does the job slower than normal concatenation:

string s = string.Empty;
int start = DateTime.Now.Millisecond;
for( int i=0;i<1000000; i++) s = "Hi " + i + "; Hi to you " + i*2;
int end = DateTime.Now.Millisecond;
Console.WriteLine("Concatenation = " + (end - start) + " millisecond");
// Concatenation = 267 millisecond

start = DateTime.Now.Millisecond;
for( int i=0;i<1000000; i++) s = string.format( "Hi {0}; Hi to you {2}", i, i*2);
end = DateTime.Now.Millisecond;
Console.WriteLine("Format = " + (end - start) + " millisecond");
// Format = 512 millisecond

Turned out concatenation is about double the speed of String.Format.

Now, it’s a fact that String.Format is slow, but did the .NET guys create this function just to sacrifice performance for a cleaner code structure? Not at all, because String.Format does much more than concatenation.

String.Format doesn’t only take simple formatting parameter like {0}, the full formatting string syntax is: {index[,length][:formatString]}.

length can be either positive or negative. It is the minimum number of characters in the string representation of the parameter.

string.Format("A {0,10} way to format", "better");
// Prints "A     better way to format".
string.Format("A {0,20} way to format", "better");
// Prints "A               better way to format".
string.Format("A {0,-20} way to format", "better");
// Prints "A better               way to format".

formatString is a string that specifies the formatting of the target object.

string.Format("This number {0:F2} is formatted", 15);
// Prints "This number 15.00 is formatted".
// F2 means Float with 2 decimals.

Possible value for formatString are the same as the values supported by the object’s ToString(format) method.

string.Format("This number {0:F2} is formatted", 15);
// This is essentially the same as:
"This number " + 15F.ToString("F2") + "is formatted";

The MSDN documentation of this function has more details if you are interested to find out.

PS. Just in case you need to write curly brackets in String.Format, you can do it using double curly brackets:

Console.WriteLine(String.Format("My name is {{0}}", "Leon"));
// Prints "My name is {0}".

See more at MSDN

Advertisements

One thought on “.NET: String.Format drama

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s