PROWAREtech

articles » current » dot-net » working-with-strings

.NET: Working with Strings

The StringBuilder class versus String.Format() plus common formatting parameters for dates and numbers written in C#.

The cleanest, easiest-to-read way to work with strings in .NET is by using String.Format() but is it the fastest.

Consider the following statement:

"The current time is: " + DateTime.Now.ToString("MM/dd/yy hh:mm:ss");

Now, using String.Format():

String.Format("The current time is: {0:MM/dd/yy hh:mm:ss}", DateTime.Now);

Performance Comparison

If performance is paramount then the StringBuilder class should be used as it is infinitely faster.

The following code takes 01:11.0532390 to execute:

int i;
string s = "";
for(i = 0; i < 100000; i++)
{
	s += " ";
}

Using String.Format() takes 02:31.1544090 to execute on the same machine:

int i;
string s = "{0}";
for(i = 0; i < 100000; i++)
{
	s = String.Format(s, " {0}");
}

Using the StringBuilder class takes 00:00.0100000 to execute on the same machine:

int i;
var sb = new StringBuilder();
for(i = 0; i < 100000; i++)
{
	sb.Append(" ");
}

The following code takes 00:08.8342978 to execute:

int i;
string s = "";
for(i = 0; i < 10000; i++)
{
	s += "How Now Brown Cow ";
}

Using String.Format() takes 00:53.8933402 to execute:

int i;
string s = "{0}";
for(i = 0; i < 10000; i++)
{
	s = String.Format(s, "How Now Brown Cow {0}");
}

The following code takes 00:08.0588880 to execute:

int i;
string s = "";
for(i = 0; i < 5000; i++)
{
	s += "abcdefghijklmnop " + "abcdefghijklmnop " + "abcdefghijklmnop " + "abcdefghijklmnop " + "abcdefghijklmnop " + "abcdefghijklmnop ";
}

The readability of String.Format() impacts performance!

Formatting Examples:

String.Format("{0:c}", 65535); // $65,535.00
String.Format("{0:c0}", 65535); // $65,535
String.Format("{0:c4}", 65535); // $65,535.0000
String.Format("{0:x}", 65535); // ffff
String.Format("{0:e}", 65535); // 6.553500e+004
String.Format("{0:d}", 65535); // 65535
String.Format("{0:g}", 65535); // 65535
String.Format("{0:f}", 65535); // 65535.00
String.Format("{0:n}", 65535); // 65,535.00
String.Format("{0:p}", 65535); // 6,553,500.00 %
String.Format("{0:g}", 65535); // 65535
String.Format("{0:00.000}", 4567.89); // 4567.890
String.Format("{0:#.##}", 4567.89); // 4567.89
String.Format("{0:0.0}", 4567.89); // 4567.9
String.Format("{0:0,0}", 4567.89); // 4,568
String.Format("{0:0,.}", 4567.89); // 5
String.Format("{0:0%}", 4567.89); // 456789%
String.Format("{0:000e+0}", 4567.89); // 457e+1
String.Format("{0:d}", DateTime.Now); // 10/21/2014
String.Format("{0:D}", DateTime.Now); // Tuesday, October 21, 2014
String.Format("{0:f}", DateTime.Now); // Tuesday, October 21, 2014 9:32 PM
String.Format("{0:F}", DateTime.Now); // Tuesday, October 21, 2014 9:32:53 PM
String.Format("{0:g}", DateTime.Now); // 10/21/2014 9:32 PM
String.Format("{0:G}", DateTime.Now); // 10/21/2014 9:32:53 PM
String.Format("{0:m}", DateTime.Now); // October 21
String.Format("{0:o}", DateTime.Now); // 2014-10-21T21:32:53.4398312-05:00
String.Format("{0:R}", DateTime.Now); // Tue, 21 Oct 2014 21:32:53 GMT
String.Format("{0:s}", DateTime.Now); // 2014-10-21T21:32:53
String.Format("{0:t}", DateTime.Now); // 9:32 PM
String.Format("{0:T}", DateTime.Now); // 9:32:53 PM
String.Format("{0:u}", DateTime.Now); // 2014-10-21 21:32:53Z
String.Format("{0:U}", DateTime.Now); // Wednesday, October 22, 2014 2:32:53 AM
String.Format("{0:Y}", DateTime.Now); // October, 2014
String.Format("{0:dd}", DateTime.Now); // 21
String.Format("{0:ddd}", DateTime.Now); // Tue
String.Format("{0:dddd}", DateTime.Now); // Tuesday
String.Format("{0:fff}", DateTime.Now); // 439
String.Format("{0:gg}", DateTime.Now); // A.D.
String.Format("{0:hh}", DateTime.Now); // 09
String.Format("{0:HH}", DateTime.Now); // 21
String.Format("{0:mm}", DateTime.Now); // 32
String.Format("{0:MM}", DateTime.Now); // 10
String.Format("{0:MMM}", DateTime.Now); // Oct
String.Format("{0:MMMM}", DateTime.Now); // October
String.Format("{0:ss}", DateTime.Now); // 53
String.Format("{0:tt}", DateTime.Now); // PM
String.Format("{0:yy}", DateTime.Now); // 14
String.Format("{0:yyyy}", DateTime.Now); // 2014
String.Format("{0:zz}", DateTime.Now); // -05
String.Format("{0:zzz}", DateTime.Now); // -05:00
String.Format("{0:hh:mm:ss.fff tt}", DateTime.Now); // 09:32:53.439 PM
String.Format("On {0:MM/dd/yy} we earned {1:c}", DateTime.Now, 65535); // On 10/21/14 we earned $65,535.00

This site uses cookies. Cookies are simple text files stored on the user's computer. They are used for adding features and security to this site. Read the privacy policy.
CLOSE