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.

examining system.string ❘ 211<br />

Hello from all the guys at Wrox Press.<br />

<br />

figure 9-1<br />

39 characters<br />

111 characters<br />

Then, on calling the AppendFormat() method, the remaining text is placed in the empty space, without the<br />

need for more memory allocation. However, the real efficiency gain from using a StringBuilder comes<br />

when you are making repeated text substitutions. For example, if you try to encrypt the text in the same<br />

way as before, you can perform the entire encryption without allocating any more memory whatsoever:<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 " +<br />

"enjoyed writing it");<br />

Console.WriteLine("Not Encoded:\n" + greetingBuilder);<br />

for(int i = 'z'; i>='a'; i--)<br />

{<br />

char old1 = (char)i;<br />

char new1 = (char)(i+1);<br />

greetingBuilder = greetingBuilder.Replace(old1, new1);<br />

}<br />

for(int i = 'Z'; i>='A'; i--)<br />

{<br />

char old1 = (char)i;<br />

char new1 = (char)(i+1);<br />

greetingBuilder = greetingBuilder.Replace(old1, new1);<br />

}<br />

Console.WriteLine("Encoded:\n" + greetingBuilder);<br />

This code uses the StringBuilder.Replace() method, which does the same thing as String.Replace(),<br />

but without copying the string in the process. The total memory allocated to hold strings in the preceding<br />

code is 150 characters for the StringBuilder instance, as well as the memory allocated during the string<br />

operations performed internally in the final Console.WriteLine() statement.<br />

Normally, you want to use StringBuilder to perform any manipulation of strings <strong>and</strong> String to store or<br />

display the final result.<br />

stringbuilder members<br />

You have seen a demonstration of one constructor of StringBuilder, which takes an initial string <strong>and</strong><br />

capacity as its parameters. There are others. For example, you can supply only a string:<br />

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

Or you can create an empty StringBuilder with a given capacity:<br />

StringBuilder sb = new StringBuilder(20);<br />

Apart from the Length <strong>and</strong> Capacity properties, there is a read-only MaxCapacity property that indicates<br />

the limit to which a given StringBuilder instance is allowed to grow. By default, this is given by int<br />

.MaxValue (roughly two billion, as noted earlier), but you can set this value to something lower when you<br />

construct the StringBuilder object:<br />

// This will both set initial capacity to 100, but the max will be 500.<br />

// Hence, this StringBuilder can never grow to more than 500 characters,<br />

// otherwise it will raise exception if you try to do that.<br />

StringBuilder sb = new StringBuilder(100, 500);<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!