Section 16.13. Insert, Remove and Replace Methods of Class StringBuilder


16.13. Insert, Remove and Replace Methods of Class StringBuilder

Class StringBuilder provides 18 overloaded Insert methods to allow values of various types to be inserted at any position in a StringBuilder. There are versions of Insert for each of the primitive types and for character arrays, Strings and Objects. Each version takes its second argument, converts it to a String and inserts the String into the StringBuilder at the index specified by the first argument. The index must be greater than or equal to 0 and less than the length of the StringBuilder; otherwise, an ArgumentOutOfRangeException occurs.

Class StringBuilder also provides method Remove for deleting characters in a StringBuilder. Method Remove takes two argumentsthe index at which to begin deletion and the number of characters to delete. The sum of the starting subscript and the number of characters to be deleted must always be less than the StringBuilder's length; otherwise, an ArgumentOutOfRangeException occurs. Figure 16.13 demonstrates the Insert and Remove methods. Lines 2037 use nine different overloaded Insert methods to insert the String representations of the variables created in lines 717 at index 0 in the StringBuilder.

Figure 16.13. StringBuilder text insertion and removal.

  1  ' Fig. 16.13: StringBuilderInsertRemove.vb  2  ' Insert and Remove methods of class StringBuilder.  3  Imports System.Text  4  5  Module StringBuilderInsertRemove  6     Sub Main()  7        Dim objectValue As Object = "hello"  8        Dim stringValue As String = "good bye"  9        Dim characterArray As Char() = _ 10           {"a"c, "b"c, "c"c, "d"c, "e"c, "f"c} 11        Dim booleanValue As Boolean = True 12        Dim characterValue As Char = "K"c 13        Dim integerValue As Integer = 7 14        Dim longValue As Long = 10000000 15        Dim floatValue As Single = 2.5F ' F indicates that 2.5 is a float 16        Dim doubleValue As Double = 33.333 17        Dim buffer As New StringBuilder() 18 19        ' insert values into buffer         20        buffer.Insert(0, objectValue)    21        buffer.Insert(0, " " )           22        buffer.Insert(0, stringValue)    23        buffer.Insert(0, " " )           24        buffer.Insert(0, characterArray) 25        buffer.Insert(0, " " )           26        buffer.Insert(0, booleanValue)   27        buffer.Insert(0, " " )           28        buffer.Insert(0, characterValue) 29        buffer.Insert(0, " " )           30        buffer.Insert(0, integerValue)   31        buffer.Insert(0, " " )           32        buffer.Insert(0, longValue)      33        buffer.Insert(0, " " )           34        buffer.Insert(0, floatValue)     35        buffer.Insert(0, " " )           36        buffer.Insert(0, doubleValue)    37        buffer.Insert(0, " " )           38 39        Console.WriteLine("buffer after inserts: " & vbCrLf & _ 40           buffer.ToString() & vbCrLf) 41 42        buffer.Remove(10, 1) ' delete 2 in 2.5 43        buffer.Remove(4, 4) ' delete .333 in 33.333 44 45        Console.WriteLine("buffer after Removes:" & vbCrLf & _ 46           buffer.ToString()) 47             ' Main 48             ' StringBuilderInsertRemove 

buffer after inserts:    33.333 2.5 10000000 7 K True abcdef good bye hello buffer after Removes:   33 .5 10000000 7 K True abcdef good bye hello 



Another useful StringBuilder method is Replace, which searches for a specified String or character and substitutes another String or character in its place. Figure 16.14 demonstrates Replace.

Figure 16.14. StringBuilder text replacement.

  1  ' Fig. 16.14: StringBuilderReplace.vb  2  ' Demonstrating method Replace.  3  Imports System.Text  4  5  Module StringBuilderReplace  6     Sub Main()  7        Dim builder1 As New StringBuilder("Happy Birthday Jane")  8        Dim builder2 As New StringBuilder("good bye greg")  9 10        Console.WriteLine("Before replacements:" & vbCrLf & _ 11           builder1.ToString() & vbCrLf & builder2.ToString()) 12 13        builder1.Replace("Jane", "Greg")      14        builder2.Replace("g"c , "G"c , 0 , 5) 15 16        Console.WriteLine(vbCrLf & "After replacements:" & vbCrLf & _ 17           builder1.ToString() & vbCrLf & builder2.ToString()) 18     End Sub ' Main 19  End Module ' StringBuilderReplace 

Happy Birthday Jane good bye greg After replacements: Happy Birthday Greg Good bye greg 



Line 13 uses method Replace to replace all instances of the String "Jane" with the String "Greg" in builder1. An overloaded version of this method takes two characters as parameters and replaces each occurrence of the first character with the second character. Line 14 uses an overload of Replace that takes four parameterstwo characters and two Integers. The method replaces all instances of the first character with the second character, beginning at the index specified by the first Integer and continuing for a count specified by the second Integer. In this case, Replace looks at only five characters, starting with the character at index 0. As the output illustrates, this version of Replace replaces g with G in the word "good", but not in "greg"the gs in "greg" are not in the range indicated by the Integer arguments (i.e., between indexes 0 and 4).



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