Insert, Remove and Replace Methods of Class StringBuilder
Class StringBuilder provides 18 overloaded Insert methods to allow various types of data to be inserted at any position in a StringBuilder. The class provides versions for each of the simple types and for character arrays, strings and objects. Each method takes its second argument, converts it to a string and inserts the string into the StringBuilder in front of the character in the position specified by the first argument. The index specified by the first argument must be greater than or equal to 0 and less than the length of the StringBuilder; otherwise, the program throws an ArgumentOutOfRangeException.
Class StringBuilder also provides method Remove for deleting any portion of 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 index and the number of characters to be deleted must always be less than the length of the StringBuilder; otherwise, the program throws an ArgumentOutOfRangeException. The Insert and Remove methods are demonstrated in Fig. 16.13.
Figure 16.13. StringBuilder text insertion and removal.
1 // Fig. 16.13: StringBuilderInsertRemove.cs 2 // Demonstrating methods Insert and Remove of the 3 // StringBuilder class. 4 using System; 5 using System.Text; 6 7 class StringBuilderInsertRemove 8 { 9 public static void Main() 10 { 11 object objectValue = "hello"; 12 string stringValue = "good bye"; 13 char[] characterArray = { 'a', 'b', 'c', 'd', 'e', 'f' }; 14 bool booleanValue = true; 15 char characterValue = 'K'; 16 int integerValue = 7; 17 long longValue = 10000000; 18 float floatValue = 2.5F; // F suffix indicates that 2.5 is a float 19 double doubleValue = 33.333; 20 StringBuilder buffer = new StringBuilder(); 21 22 // insert values into buffer 23 buffer.Insert( 0, objectValue ); 24 buffer.Insert( 0, " " ); 25 buffer.Insert( 0, stringValue ); 26 buffer.Insert( 0, " " ); 27 buffer.Insert( 0, characterArray ); 28 buffer.Insert( 0, " " ); 29 buffer.Insert( 0, booleanValue ); 30 buffer.Insert( 0, " " ); 31 buffer.Insert( 0, characterValue ); 32 buffer.Insert( 0, " " ); 33 buffer.Insert( 0, integerValue ); 34 buffer.Insert( 0, " " ); 35 buffer.Insert( 0, longValue ); 36 buffer.Insert( 0, " " ); 37 buffer.Insert( 0, floatValue ); 38 buffer.Insert( 0, " " ); 39 buffer.Insert( 0, doubleValue ); 40 buffer.Insert( 0, " " ); 41 42 Console.WriteLine( "buffer after Inserts: " + buffer + " " ); 43 44 buffer.Remove( 10, 1 ); // delete 2 in 2.5 45 buffer.Remove( 4, 4 ); // delete .333 in 33.333 46 47 Console.WriteLine( "buffer after Removes: " + buffer.ToString() ); 48 } // end method Main 49 } // end class StringBuilderInsertRemove
|
Another useful method included with StringBuilder is Replace. Replace searches for a specified string or character and substitutes another string or character in its place. Figure 16.14 demonstrates this method.
Figure 16.14. StringBuilder text replacement.
1 // Fig. 16.14: StringBuilderReplace.cs 2 // Demonstrating method Replace. 3 using System; 4 using System.Text; 5 6 class StringBuilderReplace 7 { 8 public static void Main() 9 { 10 StringBuilder builder1 = 11 new StringBuilder( "Happy Birthday Jane" ); 12 StringBuilder builder2 = 13 new StringBuilder( "good bye greg" ); 14 15 Console.WriteLine( "Before replacements: " + 16 builder1.ToString() + " " + builder2.ToString() ); 17 18 builder1.Replace( "Jane", "Greg" ); 19 builder2.Replace( 'g', 'G', 0, 5 ); 20 21 Console.WriteLine( " After replacements: " + 22 builder1.ToString() + " " + builder2.ToString() ); 23 } // end method Main 24 } // end class StringBuilderReplace
|
Line 18 uses method Replace to replace all instances of the string "Jane" with the string "Greg" in builder1. Another overload of this method takes two characters as parameters and replaces each occurrence of the first character with the second character. Line 19 uses an overload of Replace that takes four parameters, the first two of which are characters and the second two of which are ints. The method replaces all instances of the first character with the second character, beginning at the index specified by the first int and continuing for a count specified by the second int. Thus, in this case, Replace looks through 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". This is because the gs in "greg" are not in the range indicated by the int arguments (i.e., between indexes 0 and 4).