Strings are one of the most familiar, and useful, types in Visual Basic. Your average programmer simply thinks of them as a chunk of text ...
Dim myString as String = "This is a chunk of text!"
In fact, however, a String in VB.NET is more like an array of Chars (characters) than a single object. That's why you can treat it like an array as shown in the following code:
Private Sub DisplayAcronym_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim myString As String = "About Visual Basic"
MsgBox(myString.Chars(0) & _
myString.Chars(6) & _
myString.Chars(13))
End Sub
The result is shown in the illustration:
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
The Microsoft documentation describes the String object as "immutable". That means that it can't be changed. But we know that you can change a String easily in code:
myString = "A new chunk of text."
What actually happens is that VB.NET ...
- figures out how much space the new string will need
- allocates the space
- plugs the new string into the space
- releases the space that was being used by the old string
That's a lot of processing! One advantage this gives you is that a String can, in most practical situations, have unlimited length. The technical limitation stated by Microsoft is "0 to approximately 2 billion (2 ^ 31) Unicode characters." But the performance hit you take using them is usually a bigger consideration.
To solve the performance problem, VB.NET gives you a completely new object called StringBuilder. To be quite clear, a StringBuilder object is not a String. In fact, you will have to convert it to a String to use it in many situations, usually using the ToString method that all objects have.
A Stringbuilder object also has a completely new set of methods and properties. So if you are going to use it, you have to become familiar with them. The major new methods are:
- Append - This is your substitute for the "&" or "+" operator you used with String objects. We'll see how much faster it is soon.
- Length - The length of the actual text stored in the StringBuilder.
- Insert, Remove, Replace - Plugs new strings into the StringBuilder, or takes substrings out.
StringBuilder, like Strings, will automatically be reallocated by the compiled code if you try to save too much text in it. But it doesn't happen until the Capacity of the StringBuilder is actually exceeded and you can control the process with more new methods and properties. This code ...
Dim TestAppend As New System.Text.StringBuilder
MsgBox(TestAppend.Capacity.ToString)
... tells me that the initial capacity of a StringBuilder is 16 chars. If I Append a String that is 17 chars long and then check ...
Dim TestAppend As New System.Text.StringBuilder
TestAppend.Append("12345678123456781")
MsgBox(TestAppend.Capacity.ToString)
... I find that the new Capacity is 32. Microsoft states that the Capacity doubles when the old limit is reached.
But wait! There's more! You can also control the Capacity. You can set the starting Capacity and you can control the maximum Capacity. If you allocate the StringBuilder this way ...
Dim TestAppend As New System.Text.StringBuilder(10, 20)
... then it will start out with the capability of storing strings of length 10, but the statement will throw an ArgumentOutOfRangeException if you try to store a string or set the Capacity to more than 20.
On the next page, we find out just how much faster StringBuilder really is!

