Wednesday, September 24, 2008

Concatenate strings in VB.NET

VB.NET introduced a new way to concatenate strings, StringBuilder class. In VB.NET strings are immutable. This means that once a string is created its value can not be changed. So when your code concatenates strings, the strings themselves pointed by a string variable does not change. Instead a new string object is created and the string variable starts to reference this new string object.

Does this make any difference when concatenating strings in VB.NET code? Not really if you have a simple: MyString = "Hello " & "world!". But things get quite different when your code concatenates strings inside loops or you have otherwise excessive string manipulation in your code.

Concatenate strings with & -operator

Here's a simple procedure that makes concatenation in a loop with & -operator and finally shows elapsed time. Notice that the timing is done in a simple way. The timing in itself is not precise but enough to show the difference between two ways to concatenate strings.

Imports System.Text
''' <summary>
''' Concatenate strings with &amp; -operator
''' </summary>
''' <remarks></remarks>
Public Sub ConcatenateString()
  '
  Dim StartTime As DateTime
  Dim Elapsed As Double
  Dim ResultString As String
  Dim TempStr As String
  Dim i As Integer

  ResultString = ""
  TempStr = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
  StartTime = System.DateTime.Now
  For i = 0 To 9999
    ResultString = ResultString & "Line " & i & " " & TempStr & Environment.NewLine
  Next i
  Elapsed = System.DateTime.Now.Subtract(StartTime).TotalMilliseconds
  MessageBox.Show("Elapsed time " & Elapsed.ToString & " ms with & -operator", _
    "Elapsed Time", _
    MessageBoxButtons.OK, _
    MessageBoxIcon.Information)

End Sub

and the result is

Concatenate String with & -operator

Concatenate strings with StringBuilder class

Below is the same procedure as above. Now the concatenation of the strings is done with the StringBuilder object.

Imports System.Text
''' <summary>
''' Concatenate strings with StringBuilder
''' </summary>
''' <remarks></remarks>
Public Sub ConcatenateStringBuilder()
  '
  Dim StartTime As DateTime
  Dim Elapsed As Double
  Dim ResultString As String
  Dim TempStr As String
  Dim oStrBuilder As StringBuilder
  Dim i As Integer

  ResultString = ""
  TempStr = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit."
  oStrBuilder = New StringBuilder()
  StartTime = System.DateTime.Now
  For i = 0 To 9999
    oStrBuilder.Append("Line ")
    oStrBuilder.Append(i)
    oStrBuilder.Append(" ")
    oStrBuilder.Append(TempStr)
    oStrBuilder.Append(Environment.NewLine)
  Next i
  ' Move the result to ResultString variable
  ResultString = oStrBuilder.ToString
  Elapsed = System.DateTime.Now.Subtract(StartTime).TotalMilliseconds
  MessageBox.Show("Elapsed time " & Elapsed.ToString & " ms with StringBuilder", _
    "Elapsed Time", _
    MessageBoxButtons.OK, _
    MessageBoxIcon.Information)

End Sub

and now the result is

Concatenate String with StringBuilder

Conclusions

In the codes above the difference between methods was 17.5 seconds versus 0.015 seconds. So the conclusion is clear, StringBuilder class gave over 1 000 times faster results than & -operator. When your code concatenates strings inside loops or you have otherwise excessive string manipulation, use StringBuilder class to get most of your application.  But as I stated in the beginning of this post, & -operator is still useful. Using StringBuilder class in each and every simple concatenation of the strings would be an overkill.

But there's much more in StringBuilder class than just concatenating strings. Check Microsoft's reference for a complete list of the features StringBuilder class has to offer.

1 comment:

hire windows mobile developer said...

Thanks for such vital source code