The StringBuilder class stores dynamic strings and exposes methods to manipulate them much faster than the String class. As you will see, the StringBuilder class is extremely fast, but it uses considerably more memory than the string it holds. To use the StringBuilder class in an application, you must import the System.Text namespace (unless you want to fully qualify each instance of the StringBuilder class in your code). Assuming that you have imported the System.Text class in your code module, you can create a new instance of the class via the following statement:
Dim txt As New StringBuilder
Code language: PHP (php)
Because the StringBuilder class handles dynamic strings in place, it’s good to declare in advance the size of the string you intend to store in the current instance of the class. The default capacity is 16 characters, and it’s doubled automatically every time you exceed it. To set the initial capacity of the StringBuilder class, use the Capacity property.
To create a new instance of the StringBuilder class, you can call its constructor without any arguments, or pass the initial string as an argument:
Dim txt As New StringBuilder("some string")
Code language: PHP (php)
If you can estimate the length of the string you’ll store in the variable, you can specify this value by using the following form of the constructor, so that the variable need not be resized continuously as you add characters to it:
Dim txt As New StringBuilder(initialCapacity)
Code language: PHP (php)
The size you specify is not a hard limit; the variable might grow longer at runtime, and the StringBuilder will adjust its capacity.
If you want to specify a maximum capacity for your StringBuilder variable, use the following constructor:
Dim txt As New StringBuilder (intialcapacity, maxCapacity)
Code language: PHP (php)
Finally, you can initialize a new instance of the StringBuilder class by using both an initial and a maximum capacity, as well as its initial value, by using the following form of the constructor:
Dim txt As New StringBuilder(string, intialcapacity, maxCapacity)
Code language: PHP (php)
Properties
You have already seen the two basic properties of the StringBuilder class: the Capacity and MaxCapacity properties. In addition, the StringBuilder class provides the Length and Chars properties, which are the same as the corresponding properties of the String class. The Length property returns the number of characters in the current instance of the StringBuilder class, and the Chars property is an array of characters. Unlike the Chars property of the String class, this one is read/write. You can not only read individual characters, but also set them from within your code. The index of the first character is zero.
Methods
Many of the methods of the StringBuilder class are equivalent to the methods of the String class, but they act directly on the string to which they’re applied, and they don’t return a new string.
Append
The Append method appends a base type to the current instance of the StringBuilder class, and its syntax is the following, where the value argument can be a single character, a string, a date, or any numeric value:
SB.Append(value)
Code language: CSS (css)
When you append numeric values to a StringBuilder, they’re converted to strings; the value appended is the string returned by the type’s ToString method. You can also append an object to the StringBuilder — the actual string that will be appended is the value of the object’s ToString property. Another form of the Append method allows you to append an array of characters, and it has the following syntax:
SB.Append(chars, startIndex, count)
Code language: CSS (css)
Or, you can append a segment of a string by specifying the starting location of the segment in the string and the number of characters to be copied:
SB.Append(string, startIndex, count)
Code language: CSS (css)
AppendFormat
The AppendFormat method is similar to the Append method. Before appending the string, however, AppendFormat formats it. The string to be appended contains format specifications and the appropriate values. The syntax of the AppendFormat method is as follows:
SB.AppendFormat(string, values)
Code language: CSS (css)
The first argument is a string with embedded format specifications, and values is an array with values (objects, in general)— one for each format specification in the string argument. If you have a small number of values to format, up to four, you can supply them as separate arguments separated by commas:
SB.AppendFormat(string, value1, value2, value3, value4)
Code language: CSS (css)
The following statement appends the string Your balance as of Thursday, August 2, 2007 is $19,950.40 to a StringBuilder variable:
Dim statement As New StringBuilder
statement.AppendFormat( _
"Your balance as of {0:D} is ${1: #,###.00}", _
#8/2/2007#, 19950.40)
Code language: PHP (php)
Each format specification is enclosed in a pair of curly brackets, and they’re numbered sequentially (from zero). Then there’s a colon followed by the actual specification. The D format specification tells the AppendFormat method to format the specified string in long date format. The second format specification, #,###.00, uses the thousands separator and two decimal digits for the amount.
The following statements append the same string, but they pass the values through an array:
Dim accountStatement As New StringBuilder
Dim values() As Object = {#8/2/2007#, 19950.4}
accoutnStatement.AppendFormat( _
"Your balance as of {0:D} is ${1:#,###.00} ", values)
Code language: PHP (php)
In both cases, the accountStatement variable will hold a string like this one:
Your balance as of Wednesday,
August 2, 2007 is $19,950.40
Code language: JavaScript (javascript)
For more information on date and time formatting options, see the description of the ToString method of the Date type, later in this chapter.
Insert
This method inserts a string into the current instance of the StringBuilder class, and its syntax is as follows:
SB.Insert(index, value)
Code language: CSS (css)
The index argument is the location where the new string will be inserted in the current instance of the StringBuilder, and value is the string to be inserted. As with the Append method, the value argument can be any object. The Insert method will insert the string returned by the object’s ToString method. This means that you can use the Insert method to insert numeric values and dates directly into a StringBuilder variable.
A variation of the syntax shown here inserts multiple copies of the specified string into the StringBuilder:
SB.Insert(index, string, count)
Code language: CSS (css)
Yet another form of the Insert method inserts an array of characters at the location specified by the index argument in the current instance of the StringBuilder (chars is a properly declared and initialized array of characters):
SB.Insert(index, chars)
Code language: CSS (css)
Remove
This method removes a number of characters from the current StringBuilder, starting at a specified location; its syntax is the following, where startIndex is the position of the first character to be removed from the string, and count is the number of characters to be removed:
SB.Remove(startIndex, count)
Code language: CSS (css)
Replace
This method replaces all instances of a string in the current StringBuilder object with another string. The syntax of the Replace method is the following, where the two arguments can be either strings or characters:
SB.Replace(oldValue, newValue)
Code language: CSS (css)
Unlike the String class, the replacement takes place in the current instance of the StringBuilder class and the method doesn’t return another string. Another form of the Replace method limits the replacements to a specified segment of the StringBuilder instance:
SB.Replace(oldValue, newValue, startIndex, count)
Code language: CSS (css)
This method will replace all instances of oldValue with newValue in the section starting at location startIndex and extending count characters from the starting location.
ToString
Use this method to convert the StringBuilder instance to a string and assign it to a String variable. The ToString method returns the string represented by the StringBuilder variable to which it’s applied.