The & and &= operators are useful for concatenating a few strings together. However, if you must combine a large number of strings, you may get better performance using the StringBuilder class. This class is optimized for performing long sequences of concatenations to build big strings.
For small pieces of code, the difference between using a String and a StringBuilder is not noticeable. On the one hand, if you need only to concatenate a dozen or so strings once, using a StringBuilder won’t make much difference in run time. On the other hand, if you make huge strings built up in pieces, or if you build simpler strings but many times in a loop, StringBuilder may make your program run faster.
The following code concatenates the string 1234567890 a large number of times, first using a String variable and then using a StringBuilder. In one test that performed the concatenation 10,000 times to build strings 100,000 characters long, using a String took roughly 1.6 seconds. Using a StringBuilder, the program was able to build the string in roughly 0.001 seconds.
Private Sub btnGo_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnGo.Click Const ADD_STRING As String = "1234567890" Dim num_trials As Long = Long.Parse(txtNumTrials.Text) Dim start_time As DateTime Dim stop_time As DateTime Dim elapsed_time As TimeSpan Dim txt As String Dim string_builder As New StringBuilder lblString.Text = "" lblStringBuilder.Text = "" Application.DoEvents() txt = "" start_time = Now For i As Long = 1 To num_trials txt = txt & ADD_STRING Next i stop_time = Now elapsed_time = stop_time.Subtract(start_time) lblString.Text = elapsed_time.TotalSeconds.ToString("0.000000") txt = "" start_time = Now For i As Long = 1 To num_trials string_builder.Append(ADD_STRING) Next i txt = string_builder.ToString() stop_time = Now elapsed_time = stop_time.Subtract(start_time) lblStringBuilder.Text = elapsed_time.TotalSeconds.ToString("0.000000") End Sub
Admittedly, building such enormous strings is not a common programming task. Even when the strings are shorter, you can sometimes see a noticeable difference in performance. The following code concatenates the string 1234567890 to itself 100 times to build a string 1,000 characters long. It builds the string repeatedly for a certain number of trials. In one test building the 1,000 character string 10,000 times, using a String took around 0.95 seconds, whereas using a StringBuilder took about 0.06 seconds.
Private Sub btnGo_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnGo.Click Const ADD_STRING As String = "1234567890" Dim num_trials As Long = Long.Parse(txtNumTrials.Text) Dim start_time As DateTime Dim stop_time As DateTime Dim elapsed_time As TimeSpan Dim txt As String Dim string_builder As New StringBuilder lblString.Text = "" lblStringBuilder.Text = "" Application.DoEvents() start_time = Now For i As Long = 1 To num_trials txt = "" For j As Long = 1 To 100 txt = txt & ADD_STRING Next j Next i stop_time = Now elapsed_time = stop_time.Subtract(start_time) lblString.Text = elapsed_time.TotalSeconds.ToString("0.000000") txt = "" start_time = Now For i As Long = 1 To num_trials string_builder = New StringBuilder For j As Long = 1 To 100 string_builder.Append(ADD_STRING) Next j txt = string_builder.ToString() Next i stop_time = Now elapsed_time = stop_time.Subtract(start_time) lblStringBuilder.Text = elapsed_time.TotalSeconds.ToString("0.000000") End Sub
Strings and string operations are a bit more intuitive than the StringBuilder class, so your code will usually be easier to read if you use String variables when performance isn’t a big issue. If you are building enormous strings, or are building long strings a huge number of times, the performance edge given by the StringBuilder class may be worth slightly more complicated-looking code.