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

Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Flylib.com © 2008-2020.
    If you may any questions please contact us: flylib@qtcs.net