16.12. Append and AppendFormat Methods of Class StringBuilderClass StringBuilder provides 19 overloaded Append methods for appending values of various types to the end of a StringBuilder's contents. There are versions of this method for each of the primitive types and for character arrays, Strings and Objects. Remember that method ToString produces a String representation of any Object, so any Object's string representation can be appended to a StringBuilder. Each of the methods takes an argument, converts it to a String and appends it to the StringBuilder. Figure 16.11 demonstrates several Append methods. Figure 16.11. Append methods of StringBuilder.
Lines 1937 use 10 different overloaded Append methods to append the String representations of the variables created in lines 716 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 this method. Figure 16.12. StringBuilder's AppendFormat method.
Line 11 creates a String that contains formatting information. The information enclosed within the braces specifies how to format a specific piece of information. 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 formatted String. If the resulting String has fewer characters 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 that the first argument's String representation will be included in the formatted String. {1:C} specifies that the second argument will be formatted as a currency value. Line 20 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 2325 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, so the total number of formatted digits will be 3. 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 String should be aligned to the left. For more formatting options, please refer to the online help documentation. Line 28 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 versions of AppendFormat with their respective arguments. |