• No results found

Obtaining and Setting Information about StringBuilder Objects

There are several properties in the StringBuilder class you can use to obtain information about a StringBuilder object. The Length property specifies the number of characters in the current instance and the Capacity property returns the current capacity of the instance. The MaxCapacity property returns the maximum number of characters allowed in the current instance of the object (though this is automatically increased if more characters are added to the object).

The following program fragment demonstrates how to use these properties:

Dim stBuff As New StringBuilder("Stephan Jay Gould") Console.WriteLine("Length of stBuff3: " & _

Console.WriteLine("Capacity of stBuff3: " & _ stBuff.Capacity())

Console.WriteLine("Maximum capacity of stBuff3:" & _ stBuff.MaxCapacity)

The Length property can also be used to set the current length of a String- Builder object, as in the following code fragment:

stBuff.Length = 10 Console.Write(stBuff3) This code outputs

Ken Thomps

To ensure that a minimum capacity is maintained for a StringBuilder in- stance, you can call the EnsureCapacity method, passing in an integer that states the minimum capacity for the object. Here’s an example:

stBuff.EnsureCapacity(25)

Another property you can use is the Chars property. This property either returns the character in the position specified in its argument or sets the character passed as an argument. The following code shows a simple example using the Chars property:

Dim stBuff As New StringBuilder("Ronald Knuth") If (stBuff.Chars(0) <> "D"c) Then

stBuff.Chars(0) = "D" End If

Modifying StringBuffer Objects

We can modify a StringBuilder object by appending new characters to the end of the object, inserting characters into an object, replacing a set of char- acters in an object with different characters, and removing characters from an object. We discuss the methods responsible for these operations in this section.

You can add characters to the end of a StringBuilder object by using the Append method. This method takes a string value as an argument and

The StringBuilder Class 173

concatenates the string to the end of the current value in the object. The following program demonstrates how the Append method works:

Imports System.text Module Module1

Sub Main()

Dim stBuff As New StringBuilder() Dim words() As String = _

{"now ", "is ", "the ", "time ", "for ", "all ", _ "good ", "men ", "to ", "come ", "to ", "the ", _ "aid ", "of ", "their ", "party"}

Dim index As Integer

For index = 0 To words.GetUpperBound(0) stBuff.Append(words(index)) Next Console.WriteLine(stBuff) Console.Read() End Sub End Module

The output is, of course,

Now is the time for all good men to come to the aid of their party

A formatted string can be appended to a StringBuilder object. A formatted string is a string that includes a format specification embedded in the string. There are too many format specifications to cover in this section, so we’ll just demonstrate a common specification. We can place a formatted number within a StringBuilder object like this:

Imports System.text Module Module1

Sub Main()

Dim stBuff As New StringBuilder Console.WriteLine()

stBuff.AppendFormat _

stBuff.AppendFormat(Constants.vbCrLf & _

"We have{0000}widgets left.",12) Console.WriteLine(stBuff)

Console.Read() End Sub

End Module

The output from this program looks like this:

The format specification is enclosed within curly braces that are embedded in a string literal. The data after the comma are placed into the specification when the code is executed. See the VB.NET documentation for a complete list of format specifications.

Next we consider the Insert method. This method allows us to insert a string into the current StringBuilder object. The method can take up to three arguments. The first argument specifies the position to begin the insertion. The second argument is the string you want to insert. The third argument, which is optional, is an integer that specifies the number of times you want to insert the string into the object.

Here’s a small program that demonstrates how to use the Insert method: Imports System.text

Module Module1 Sub Main()

Dim stBuff As New StringBuilder stBuff.Insert(0, "Hello")

stBuff.Append("world") stBuff.Insert(5, ", ") Console.WriteLine(stBuff)

Dim chars() As Char = {"t"c, "h"c, "e"c, "r"c, "e"} stBuff.Insert(5, " " & chars)

The StringBuilder Class 175 Console.Read() End Sub End Module The output is Hello, world

Hello there, world

The following program utilizes the Insert method using the third argument for specifying the number of insertions to make:

Dim stBuff As New StringBuilder stBuff.Insert(0, "and on ", 6) Console.WriteLine(stBuff) Here’s the output:

and on and on and on and on and on and on

The StringBuilder class has a Remove method for removing characters from a StringBuilder object. This method takes two arguments: a starting position and the number of characters to remove. Here’s how it works:

Dim stBuff As New StringBuilder("noise in +++++string") stBuff.Remove(9, 5)

Console.WriteLine(stBuff) The program outputs

noise in string

You can replace characters in a StringBuilder object with the Replace method. This method takes two arguments: the old string to replace and the new string to put in its place. The following code fragment demonstrates how the method works:

Dim stBuff As New StringBuilder("recieve decieve reciept") stBuff.Replace("cie", "cei")

Console.WriteLine(stBuff) Each “cie” is replaced with “cei”.

When working with StringBuilder objects, you will often want to convert them to strings, perhaps to use a method that isn’t found in the StringBuilder class. You can do this with the ToString method. This methods returns a String instance of the current StringBuilder instance. Here’s an example:

Imports System.text Module Module1

Sub Main()

Dim stBuff As New StringBuilder("HELLO WORLD") Dim st As String = stBuff.ToString()

st = st.ToLower() st = st.Replace(st.Substring(0, 1), _ st.Substring(0, 1). ToUpper()) stBuff.Replace(stBuff.ToString, st) Console.WriteLine(stBuff) Console.Read() End Sub End Module

This program displays the string, “Hello world” by first converting stBuff to a string (the st variable), making all the characters in the string lowercase, capitalizing the first letter in the string, and then replacing the old string in the StringBuilder object with the value of st. The ToString method is used in the first argument of Replace because the first parameter is supposed to be a string. You can’t call the StringBuilder object directly here.

COMPARING THE

EFFICIENCY OF THESTRING