If either an append or insert operation
forces the need for the buffer to grow greater than the maximum capacity, an
ArgumentOutOfRangeException will be thrown.
For convenience, all the methods that append and insert data into an instance return a
reference to the StringBuilder instance. Thus you can chain operations on a single string
builder as shown:
Imports System
Imports System.Text
Public Class EntryPoint
Shared Sub Main()
Dim sb As StringBuilder = New StringBuilder()
sb.Append("StringBuilder ").Append("is ").Append("very . . . ")
CHAPTER 9 n WORKING WITH STRINGS 184
Dim built1 As String = sb.ToString()
sb.Append("cool")
Dim built2 As String = sb.ToString()
Console.WriteLine(built1)
Console.WriteLine(built2)
End Sub
End Class
Here are the results from running the previous code:
StringBuilder is very . . .
StringBuilder is very . . . cool
In the previous example, we converted the StringBuilder instance sb into a new
System.String instance named built1 by calling sb.ToString(). For maximum efficiency, the
StringBuilder simply hands off a reference to the character buffer to the string instance so
that a copy is not necessary. If you think about it, part of the utility of StringBuilder would
be compromised if it didn??™t do it this way.
Pages:
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312