Working with Strings in ASP.NET - String.Format() and StringBuilder class

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:

Dim i As Integer
Dim s As String = ""
For i = 0 to 99999
	s &= " "
Next i

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

Dim i As Integer
Dim s As String = "{0}"
For i = 0 to 99999
	s = String.Format(s, " {0}")
Next i

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

Dim i As Integer
Dim sb As New StringBuilder();
For i = 0 to 99999
	sb.Append(" ")
Next i

The following code takes 00:08.8342978 to execute:

Dim i As Integer
Dim s As String = ""
For i = 0 to 9999
	s &= "How Now Brown Cow "
Next i

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

Dim i As Integer
Dim s As String = "{0}"
For i = 0 To 9999
	s = String.Format(s, "How Now Brown Cow {0}")
Next i

The following code takes 00:08.0588880 to execute:

Dim i As Integer
Dim s As String = ""
For i = 0 To 4999
	s &= "abcdefghijklmnop " & "abcdefghijklmnop " & "abcdefghijklmnop " & _
	"abcdefghijklmnop " & "abcdefghijklmnop " & "abcdefghijklmnop "
Next i

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

Dim i As Integer
Dim Args(5) As String
Args(0) = "abcdefghijklmnop "
Args(1) = "abcdefghijklmnop "
Args(2) = "abcdefghijklmnop "
Args(3) = "abcdefghijklmnop "
Args(4) = "abcdefghijklmnop "
Args(5) = "abcdefghijklmnop {0}{1}{2}{3}{4}{5}"
Dim s As String = "{0}{1}{2}{3}{4}{5}"
For i = 0 To 4999
	s = String.Format(s, Args)
Next i

The flexibility of String.Format() impacts performance.

Some 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