Append and AppendFormat Methods of Class StringBuilder

Class StringBuilder provides 19 overloaded Append methods that allow various types of values to be added to the end of a StringBuilder. The FCL provides versions for each of the simple types and for character arrays, strings and objects. (Remember that method ToString produces a string representation of any object.) Each of the methods takes an argument, converts it to a string and appends it to the StringBuilder. Figure 16.11 demonstrates the use of several Append methods.

Figure 16.11. Append methods of StringBuilder.

1 // Fig. 16.11: StringBuilderAppend.cs 2 // Demonstrating StringBuilder Append methods. 3 using System; 4 using System.Text; 5 6 class StringBuilderAppend 7 { 8 public static void Main( string[] args ) 9 { 10 object objectValue = "hello"; 11 string stringValue = "good bye"; 12 char[] characterArray = { 'a', 'b', 'c', 'd', 'e', 'f' }; 13 bool booleanValue = true; 14 char characterValue = 'Z'; 15 int integerValue = 7; 16 long longValue = 1000000; 17 float floatValue = 2.5F; // F suffix indicates that 2.5 is a float 18 double doubleValue = 33.333; 19 StringBuilder buffer = new StringBuilder(); 20 21 // use method Append to append values to buffer 22 buffer.Append( objectValue ); 23 buffer.Append( " " ); 24 buffer.Append( stringValue ); 25 buffer.Append( " " ); 26 buffer.Append( characterArray ); 27 buffer.Append( " " ); 28 buffer.Append( characterArray, 0, 3 ); 29 buffer.Append( " " ); 30 buffer.Append( booleanValue ); 31 buffer.Append( " " ); 32 buffer.Append( characterValue ); 33 buffer.Append( " " ); 34 buffer.Append( integerValue ); 35 buffer.Append( " " ); 36 buffer.Append( longValue ); 37 buffer.Append( " " ); 38 buffer.Append( floatValue ); 39 buffer.Append( " " ); 40 buffer.Append( doubleValue ); 41 42 Console.WriteLine( "buffer = " + buffer.ToString() + " " ); 43 } // end method Main 44 } // end class StringBuilderAppend

buffer = hello good bye abcdef abc True Z 7 1000000 2.5 33.333

Lines 2240 use 10 different overloaded Append methods to attach the string representations of objects created in lines 1018 to the end of the StringBuilder. Append behaves similarly to the + operator, which is used to concatenate strings.

Class StringBuilder also provides method AppendFormat, which converts a string to a specified format, then appends it to the StringBuilder. The example in Fig. 16.12 demonstrates the use of this method.

Figure 16.12. StringBuilder's AppendFormat method.

(This item is displayed on pages 784 - 785 in the print version)

1 // Fig. 16.12: StringBuilderAppendFormat.cs 2 // Demonstrating method AppendFormat. 3 using System; 4 using System.Text; 5 6 class StringBuilderAppendFormat 7 { 8 public static void Main( string[] args ) 9 { 10 StringBuilder buffer = new StringBuilder(); 11 string string1, string2; 12 13 // formatted string 14 string1 = "This {0} costs: {1:C}. "; 15 16 // string1 argument array 17 object[] objectArray = new object[ 2 ]; 18 19 objectArray[ 0 ] = "car"; 20 objectArray[ 1 ] = 1234.56; 21 22 // append to buffer formatted string with argument 23 buffer.AppendFormat( string1, objectArray ); 24 25 // formatted strings 26 string2 = "Number:{0:d3}. " + 27 "Number right aligned with spaces:{0, 4}. " + 28 "Number left aligned with spaces:{0, -4}."; 29 30 // append to buffer formatted string with argument 31 buffer.AppendFormat( string2, 5 ); 32 33 // display formatted strings 34 Console.WriteLine( buffer.ToString() ); 35 } // end method Main 36 } // end class StringBuilderAppendFormat

This car costs: $1,234.56. Number:005. Number right aligned with spaces: 5. Number left aligned with spaces:5 .

Line 14 creates a string that contains formatting information. The information enclosed in braces specifies how to format a specific piece of data. Formats have the form {X[,Y][:FormatString]}, where X is the number of the argument to be formatted, counting from zero. Y is an optional argument, which can be positive or negative, indicating how many characters should be in the result. If the resulting string is less than the number Y, the string will be padded with spaces to make up for the difference. A positive integer aligns the string to the right; a negative integer aligns it to the left. The optional FormatString applies a particular format to the argumentcurrency, decimal or scientific, among others. In this case, "{0}" means the first argument will be printed out. "{1:C}" specifies that the second argument will be formatted as a currency value.

Line 23 shows a version of AppendFormat that takes two parametersa string specifying the format and an array of objects to serve as the arguments to the format string. The argument referred to by "{0}" is in the object array at index 0.

Lines 2628 define another string used for formatting. The first format "{0:d3}", specifies that the first argument will be formatted as a three-digit decimal, meaning any number that has fewer than three digits will have leading zeros placed in front to make up the difference. The next format, "{0, 4}", specifies that the formatted string should have four characters and should be right aligned. The third format, "{0, -4}", specifies that the strings should be aligned to the left. For more formatting options, please refer to the online help documentation.

Line 31 uses a version of AppendFormat that takes two parametersa string containing a format and an object to which the format is applied. In this case, the object is the number 5. The output of Fig. 16.12 displays the result of applying these two versions of AppendFormat with their respective arguments.

Insert, Remove and Replace Methods of Class StringBuilder

Категории