Recipe 5.26. Concatenating Strings


Problem

You want to concatenate strings quickly and efficiently.

Solution

Sample code folder: Chapter 05\Concatenate

Use the &= concatenation shortcut, or, even better, use a StringBuilder.

Discussion

Visual Basic 2005 offers a few tricks for working with strings more efficiently. The following code presents several helpful techniques, from least to most efficient.

This approach simply concatenates two words and assigns the resulting string to a string variable:

 Dim quote As String quote = "The " & "important " 

This is how additional string data was always concatenated to the end of a string in VB 6 and earlier versions of the BASIC language:

 quote = quote & "thing " 

Because .NET strings are immutable, this code copies the current contents of quote to a new location in memory, then copies the short string "thing " to its tail end, and finally assigns the address of the resulting string to the quote variable, marking the previous contents of quote for garbage collection. By the time you've repeat this type of command a few times to concatenate more strings to the tail end of quote, a lot of bytes have gotten shuffled in memory.

This newer technique, available in Visual Basic 2005, provides an improved syntax, although timing tests seem to indicate that a lot of string data is still being shuffled in memory:

 quote &= "is not to stop questioning. " quote &= "--Albert Einstein" 

The StringBuilder is by far the better way to proceed when concatenating many strings end to end, and you'll find a lot of examples of its use in this book. As shown here, you can run the Append() method on the results of another Append(), which may or may not make it easier to read the code:

 Dim result As New _    System.Text.StringBuilder("The important thing ") result.Append("is questioning. ") result.Append("--").Append("Albert ").Append("Einstein") 

As explained in Recipe 5.1, the StringBuilder maintains an internal buffer of characters, not a true string, and the buffer grows by doubling in size whenever room runs out during an Append() operation. String data is concatenated in place in memory, which keeps the total clock cycles for concatenation way down compared to standard string techniques.

Just to round things out, these last few lines show some of the additional commands available when working with a StringBuilder:

 result.Insert(23, "note to stop ") result.Replace("note", "not") result.Insert(0, quote & vbNewLine) MsgBox(result.ToString()) 

These lines complete the building of the string data displayed by the message box shown in Figure 5-29. The two strings demonstrate that identical results are obtained even after we've manipulated the StringBuilder's contents.

Figure 5-29. The string built up using a StringBuilder


See Also

Recipe 5.1 and Recipe 5.27 discuss the StringBuilder class in more detail.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net