15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

For simplicity, this code does not wrap Z to A or z to a. These letters get encoded to<br />

[ <strong>and</strong> { , respectively.<br />

In this example, the Replace() method works in a fairly intelligent way, to the extent that it won ’ t actually<br />

create a new string unless it actually makes changes to the old string. The original string contained 23<br />

different lowercase characters <strong>and</strong> 3 different uppercase ones. The Replace() method will therefore have<br />

allocated a new string 26 times in total, with each new string storing 103 characters. That means that<br />

because of the encryption process, there will be string objects capable of storing a combined total of 2,678<br />

characters now sitting on the heap waiting to be garbage - collected! Clearly, if you use strings to do text<br />

processing extensively, your applications will run into severe performance problems.<br />

To address this kind of issue, Microsoft has supplied the System.Text.StringBuilder class. StringBuilder<br />

is not as powerful as String in terms of the number of methods it supports. The processing you can do on a<br />

StringBuilder is limited to substitutions <strong>and</strong> appending or removing text from strings. However, it works in<br />

a much more effi cient way.<br />

When you construct a string using the String class, just enough memory is allocated to hold the string. The<br />

StringBuilder , however, normally allocates more memory than is actually needed. You, as a developer,<br />

have the option to indicate how much memory the StringBuilder should allocate, but if you do not, the<br />

amount will default to some value that depends on the size of the string that the StringBuilder instance is<br />

initialized with. The StringBuilder class has two main properties:<br />

➤<br />

➤<br />

Length , which indicates the length of the string that it actually contains<br />

Capacity , which indicates the maximum length of the string in the memory allocation<br />

Any modifi cations to the string take place within the block of memory assigned to the StringBuilder<br />

instance, which makes appending substrings <strong>and</strong> replacing individual characters within strings very<br />

effi cient. Removing or inserting substrings is inevitably still ineffi cient because it means that the following<br />

part of the string has to be moved. Only if you perform some operation that exceeds the capacity of the<br />

string is it necessary to allocate new memory <strong>and</strong> possibly move the entire contained string. In adding<br />

extra capacity, based on our experiments the StringBuilder appears to double its capacity if it detects the<br />

capacity has been exceeded <strong>and</strong> no new value for the capacity has been set.<br />

For example, if you use a StringBuilder object to construct the original greeting string, you might<br />

write this code:<br />

StringBuilder greetingBuilder =<br />

new StringBuilder("Hello from all the guys at Wrox Press. ", 150);<br />

greetingBuilder.AppendFormat("We do hope you enjoy this book as much as we enjoyed<br />

writing it");<br />

To use the StringBuilder class, you need a System.Text reference in your code.<br />

This code sets an initial capacity of 150 for the StringBuilder . It is always a good idea to set some<br />

capacity that covers the likely maximum length of a string, to ensure the StringBuilder does not need to<br />

relocate because its capacity was exceeded. By default, the capacity is set to 16. Theoretically, you can set as<br />

large a number as you can pass in an int , although the system will probably complain that it does not have<br />

enough memory if you actually try to allocate the maximum of two billion characters (this is the theoretical<br />

maximum that a StringBuilder instance is allowed to contain, in principle).<br />

When the preceding code is executed, it fi rst creates a StringBuilder object that looks like Figure 9 - 1.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!