Visual Basic 2005 for Programmers (2nd Edition)
16.11. Length and Capacity Properties, EnsureCapacity Method and Indexer of Class StringBuilder
Class StringBuilder provides the Length and Capacity properties to return the number of characters currently in a StringBuilder and the number of characters that a StringBuilder can store without allocating more memory, respectively. These properties also can increase or decrease the length or the capacity of the StringBuilder. Method EnsureCapacity allows you to reduce the number of times a StringBuilder's capacity must be increased. Method EnsureCapacity doubles the StringBuilder instance's current capacity. If this doubled value is greater than the value you wish to ensure, that value becomes the new capacity. Otherwise, EnsureCapacity alters the capacity by making it equal to the requested number. For example, if the current capacity is 17 and we wish to make it 40, 17 multiplied by 2 is not greater than or equal to 40, so the call will result in a new capacity of 40. If the current capacity is 23 and we wish to make it 40, 23 will be multiplied by 2 to result in a new capacity of 46. Both 40 and 46 are greater than or equal to 40, so a capacity of 40 is indeed ensured by method EnsureCapacity. Figure 16.10 demonstrates these methods and properties. Figure 16.10. StringBuilder size manipulation.
The program contains one StringBuilder, called buffer. Line 7 uses the StringBuilder constructor that takes a String argument to instantiate a StringBuilder and initialize its value to "Hello, how are you?". Lines 1012 output the content, length and capacity of the StringBuilder. In the output window, note that the capacity of the StringBuilder is initially 32. Remember, the StringBuilder constructor that takes a String argument creates a StringBuilder object with an initial capacity that is the smallest power of two greater than or equal to the number of characters in the String passed as an argument. Line 14 expands the capacity of the StringBuilder to a minimum of 75 characters. The current capacity (32) multiplied by two is less than 75, so method EnsureCapacity increases the capacity to 75. If new characters are added to a StringBuilder so that its length exceeds its capacity, the capacity grows to accommodate the additional characters in the same manner as if method EnsureCapacity had been called. Line 18 uses property Length to set the length of the StringBuilder to 10. If the specified length is less than the current number of characters in the StringBuilder, the contents of the StringBuilder are truncated to the specified length. If the specified length is greater than the number of characters currently in the StringBuilder, null characters (characters with the numeric representation 0) are appended to the StringBuilder until the total number of characters in the StringBuilder is equal to the specified length. Lines 2325 use the StringBuilder indexer to output the characters in the StringBuilder one character at a time. |