String Concatenation...
Learn some of the methods of string concatenation in .NET.
The .NET framework has implemented many JavaScript-like methods for string manipulations. These methods are shared and are invoked by the string class. The following are examples of uses for the Substring method:
String.Substring(strText, 3)
strText.Substring(3)
"This is some text".Substring(3)
In addition, .NET still supports string concatenation using the ampersand character (plus character for C#), although it isn't the most efficient method. You can append as many strings to the end of an existing string objects as such:
strText &= "First part of text" & "second part of text" & "third part of text"
Each of the parts of text added to the string variable creates a new string object. So "First part of text", "second part of text", and "third part of text" implement new string variables, which can hinder performance. .NET has implemented a new StringBuilder class that solves these performance issues. By using the Append method, text is appended to it in the same manner as shown above. The StringBuilder version of the example shown above would appear as:
Dim objBuilder As New StringBuilder
With objBuilder
.Append("First part of text")
.Append("second part of text")
.Append("third part of text")
End With
To retrieve the entire appended string, use the ToString() method. Not only does this class have access to string manipulation methods through the String class, you also have methods implemented through the StringBuilder class at your advantage.
In addition to appending text, the StringBuilder supports insertion, removal, and replacement of text via the Insert, Remove, and Replace methods. The Insert method inserts a string into the StringBuilder at a designated index value. All characters to the right of the index value are shifted to make room for the inserted text. In addition, there are many overloaded versions of the Insert method to support multiple data types, such as Char, Integer, Boolean, etc.
The Remove method takes an index value denoting the first character to start at and a length specifying the total number of characters to remove. The characters after the removed text are shifted left, which will start at the specified index value. The Replace method is the same as the String class's, and will not be discussed in this article.
m_objBuilder.Insert(1, txtText.Text)
m_objBuilder.Remove(1, 6)
In addition to string manipulation, the StringBuilder object can limit the number of characters it can hold, which these values can be altered at the constructor level. The Constructor (one of them) takes two integer parameters, which affect the total storage limits of the StringBuilder. The first parameter, Capacity, sets the appropriate starting size for the class, while the next parameter, MaxCapacity, determines the total amount of characters allowed. Using these parameters can restrict the total size of the StringBuilder.
m_objBuilder = New StringBuilder(100, 1000)
This article is a short and simple article, and is meant to show you the capabilities of the StringBuilder class, and of the new string objects that the .NET framework has to offer.
You may download the example code here.