Section 16.10. Class StringBuilder


16.10. Class StringBuilder

The String class provides many capabilities for processing Strings. However, a String's contents can never change. Operations that seem to concatenate Strings are in fact creating new Strings (e.g., the &= operator creates a new String and assigns it to the String variable on the left side of the operator).

The next several sections discuss the features of class StringBuilder (namespace System.Text), used to create and manipulate dynamic string informationthat is, mutable strings. Every StringBuilder can store the number of characters specified by its capacity. Exceeding the capacity of a StringBuilder makes the capacity expand to accommodate the additional characters. As we will see, members of class StringBuilder, such as methods Append and AppendFormat, can be used for concatenation like the operators &, and &= for class String.

Performance Tip 16.2

Objects of class String are constant strings, whereas object of class StringBuilder are mutable sequences of characters. The CLR can perform certain optimizations with Strings (such as referring to one String with many variables) because it knows the Strings will not change.


Performance Tip 16.3

When you have a choice between using a String or a StringBuilder to represent a sequence of characters, always use a String if the contents of the object will not change. When appropriate, using Strings instead of StringBuilder objects improves performance.


Class StringBuilder provides six overloaded constructors. Class StringBuilderConstructor (Fig. 16.9) demonstrates three of them.

Figure 16.9. StringBuilder class constructors.

  1  ' Fig. 16.9: StringBuilderConstructor.vb  2  ' Demonstrating StringBuilder class constructors.  3  Imports System.Text  4  5  Module StringBuilderConstructor  6     Sub Main()  7        Dim buffer1, buffer2, buffer3 As StringBuilder  8  9        buffer1 = New StringBuilder() 10        buffer2 = New StringBuilder(10) 11        buffer3 = New StringBuilder("hello") 12 13        Console.WriteLine("buffer1 = """ & buffer1.ToString() & """") 14        Console.WriteLine("buffer2 = """ & buffer2.ToString() & """") 15        Console.WriteLine("buffer3 = """ & buffer3.ToString() & """") 16      End Sub ' Main 17  End Module ' StringBuilderConstructor 

 buffer1 = "" buffer2 = "" buffer3 = "hello" 



Line 9 employs the parameterless StringBuilder constructor to create a StringBuilder that contains no characters and has a default initial capacity of 16 characters. Line 10 uses the StringBuilder constructor that takes an Integer argument to create a StringBuilder that contains no characters and has the initial capacity specified in the Integer argument (i.e., 10). Line 11 uses the StringBuilder constructor that takes a String argument to create a StringBuilder containing the characters of the String argument. Theinitial capacity is the smallest power of two greater than or equal to the number of characters in the argument String, with a minimum of 16. Lines 1315 use StringBuilder method ToString to obtain String representations of the StringBuilders' contents.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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