The string class provides many capabilities for processing strings. However a string's contents can never change. Operations that seem to concatenate strings are in fact assigning string references to newly created strings (e.g., the += operator creates a new string and assigns the initial string reference to the newly created string).
The next several sections discuss the features of class StringBuilder (namespace System.Text), used to create and manipulate dynamic string informationi.e., mutable strings. Every StringBuilder can store a certain number of characters that is specified by its capacity. Exceeding the capacity of a StringBuilder causes the capacity to expand to accommodate the additional characters. As we will see, members of class StringBuilder, such as methods Append and AppendFormat, can be used for concatenation like the operators + and += for class string.
Class StringBuilder provides six overloaded constructors. Class StringBuilderConstructor (Fig. 16.9) demonstrates three of these overloaded constructors.
Figure 16.9. StringBuilder class constructors.
1 // Fig. 16.9: StringBuilderConstructor.cs 2 // Demonstrating StringBuilder class constructors. 3 using System; 4 using System.Text; 5 6 class StringBuilderConstructor 7 8 public static void Main() 9 { 10 StringBuilder buffer1, buffer2, buffer3; 11 12 buffer1 = new StringBuilder(); 13 buffer2 = new StringBuilder( 10 ); 14 buffer3 = new StringBuilder( "hello" ); 15 16 Console.WriteLine( "buffer1 = "" + buffer1 + """ ); 17 Console.WriteLine( "buffer2 = "" + buffer2 + """ ); 18 Console.WriteLine( "buffer3 = "" + buffer3 + """ ); 19 } // end method Main 20 } // end class StringBuilderConstructor
|
Line 12 employs the no-parameter StringBuilder constructor to create a StringBuilder that contains no characters and has a default initial capacity of 16 characters. Line 13 uses the StringBuilder constructor that takes an int argument to create a StringBuilder that contains no characters and has the initial capacity specified in the int argument (i.e., 10). Line 14 uses the StringBuilder constructor that takes a string argument to create a StringBuilder containing the characters of the string argument. The initial capacity is the smallest power of two greater than or equal to the number of characters in the argument string, with a minimum of 16. Lines 1618 implicitly use StringBuilder method ToString to obtain string representations of the StringBuilders' contents.
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