16.12 Append and AppendFormat Methods of Class StringBuilder

Class StringBuilder provides overloaded Append methods that allow various types of values to be added to the end of a StringBuilder. The Framework Class Library provides versions for each simple type and for character arrays, strings and objects. (Remember that method ToString produces a string representation of any object.) Each method takes an argument, converts it to a string and appends it to the StringBuilder. Figure 16.11 uses several Append methods (lines 22–40) to attach the variables’ string representations in lines 10–18 to the end of the StringBuilder.

Fig. 16.11 Demonstrating StringBuilder Append methods.

Alternate View

 1    // Fig. 16.11: StringBuilderAppend.cs
 2    // Demonstrating StringBuilder Append methods.
 3    using System;
 4    using System.Text;
 5
 6    class StringBuilderAppend
 7    {
 8       static void Main()
 9       {
10          object objectValue = "hello";
11          var stringValue = "good bye";
12          char[] characterArray = {'a', 'b', 'c', 'd', 'e', 'f'};
13          var booleanValue = true;
14          var characterValue = 'Z';
15          var integerValue = 7;
16          var longValue = 1000000L; // L suffix indicates a long literal
17          var floatValue = 2.5F; // F suffix indicates a float literal
18          var doubleValue = 33.333;
19          var 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        }
44    }

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

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 AppendFormat.

Fig. 16.12 Demonstrating method AppendFormat.

Alternate View

 1    // Fig. 16.12: StringBuilderAppendFormat.cs
 2    // Demonstrating method AppendFormat.
 3    using System;
 4    using System.Text;
 5
 6    class StringBuilderAppendFormat
 7    {
 8       static void Main()
 9       {
10          var buffer = new StringBuilder();
11
12          // formatted string
13          var string1 = "This {0} costs: {1:C}.

";
14
15          // string1 argument array
16          var objectArray = new object[2] {"car", 1234.56};
17
18          // append to buffer formatted string with argument
19          buffer.AppendFormat(string1, objectArray);
20
21          // formatted string
22          string string2 = "Number:
{0:d3}.

" +           
23             "Number right aligned with spaces:
{0,4}.

" +
24             "Number left aligned with spaces:
{0,-4}.";     
25
26          // append to buffer formatted string with argument
27          buffer.AppendFormat(string2, 5);
28
29          // display formatted strings
30          Console.WriteLine(buffer.ToString());
31       }
32    }

This car costs: $1,234.56.

Number:
005.

Number right aligned with spaces:
   5.

Number left aligned with spaces:
5   .

Line 13 declares a format string that consists of text and format items. Each format item in braces ({}) is a placeholder for a value. Format items also may include the same optional formatting you’ve seen throughout this book in interpolated strings. Line 16 declares and initializes an array of objects that will be formatted. Line 19 shows a version of AppendFormat that takes two parameters—a format string and an array of objects to serve as the arguments to the format string. The object at index 0 of the array is formatted by the format item "{0}", which simply produces the object’s string representation. The object at index 1 of the array is formatted by the format item "{1:C}", which formats the object as currency.

Lines 22–24 declare another format string with three format specifiers:

  • The first—{0:d3}—formats a three-digit integer value. Any number having fewer than three digits will have leading zeros.

  • The second—{0,4}—formats a string in a right-aligned field of four characters.

  • The third—{0,-4}—formats a string in a left-aligned field of four characters.

Line 27 uses a version of AppendFormat that takes two parameters—a format string and an object to format. In this case, the object is the number 5, which is formatted by all three format specifiers. The output displays the result of applying these two versions of Append-Format with their respective arguments.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset