15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

212 ❘ ChaPTer 9 strinGs And reGulAr expressiOns<br />

You can also explicitly set the capacity at any time, though an exception will be raised if you set it to a value<br />

less than the current length of the string or a value that exceeds the maximum capacity:<br />

StringBuilder sb = new StringBuilder("Hello");<br />

sb.Capacity = 100;<br />

The following table lists the main StringBuilder methods.<br />

meThod<br />

Append()<br />

AppendFormat()<br />

Insert()<br />

Remove()<br />

Replace()<br />

ToString()<br />

P u r P o s e<br />

Appends a string to the current string.<br />

Appends a string that has been worked out from a format specifi er.<br />

Inserts a substring into the current string.<br />

Removes characters from the current string.<br />

Replaces all occurrences of a character with another character or a substring with<br />

another substring in the current string.<br />

Returns the current string cast to a ystem.String S<br />

object (overridden from<br />

System.Object ).<br />

Several overloads of many of these methods exist.<br />

AppendFormat() is actually the method that is ultimately called when you call<br />

Console . WriteLine() , which has responsibility for working out what all the format<br />

expressions like {0:D} should be replaced with. This method is examined in the<br />

next section.<br />

There is no cast (either implicit or explicit) from StringBuilder to String . If you want to output the<br />

contents of a StringBuilder as a String , you must use the ToString() method.<br />

Now that you have been introduced to the StringBuilder class <strong>and</strong> have learned some of the ways in<br />

which you can use it to increase performance, you should be aware that this class will not always give you<br />

the increased performance you are looking for. Basically, the StringBuilder class should be used when you<br />

are manipulating multiple strings. However, if you are just doing something as simple as concatenating two<br />

strings, you will fi nd that System.String will be better - performing.<br />

format strings<br />

So far, a large number of classes <strong>and</strong> structs have been written for the code samples presented in this book,<br />

<strong>and</strong> they have normally implemented a ToString() method in order to display the contents of a given<br />

variable. However, quite often users might want the contents of a variable to be displayed in different, often<br />

culture - <strong>and</strong> locale - dependent ways. The .<strong>NET</strong> base class, System.DateTime , provides the most obvious<br />

example of this. For example, you might want to display the same date as 10 June 2010, 10 Jun 2010,<br />

6/10/10 (USA), 10/6/10 (UK), or 10.06.2010 (Germany).<br />

Similarly, the Vector struct in Chapter 7, “ Operators <strong>and</strong> Casts ,” implements the Vector.ToString()<br />

method to display the vector in the format (4, 56, 8) . There is, however, another very common way of<br />

writing vectors, in which this vector would appear as 4i + 56j + 8k . If you want the classes that you<br />

write to be user - friendly, they need to support the facility to display their string representations in any of the<br />

formats that users are likely to want to use. The .<strong>NET</strong> runtime defi nes a st<strong>and</strong>ard way in which this should<br />

be done: the IFormattable interface. Showing how to add this important feature to your classes <strong>and</strong> structs<br />

is the subject of this section.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!