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.

9strings <strong>and</strong> regular expressions<br />

WhaT ’ s in This ChaPTer<br />

➤<br />

➤<br />

➤<br />

Building strings<br />

Formatting expressions<br />

Using regular expressions<br />

Since the beginning of this book, you have been using strings almost constantly, <strong>and</strong> might not have<br />

realized that the stated mapping that the string keyword in <strong>C#</strong> actually refers to is the System<br />

.String .<strong>NET</strong> base class. System.String is a very powerful <strong>and</strong> versatile class, but it is by no means<br />

the only string - related class in the .<strong>NET</strong> armory. This chapter starts by reviewing the features of<br />

System.String <strong>and</strong> then looks at some nifty things you can do with strings using some of the other<br />

.<strong>NET</strong> classes — in particular those in the System.Text <strong>and</strong> System.Text.RegularExpressions<br />

namespaces. This chapter covers the following areas:<br />

➤<br />

➤<br />

➤<br />

Building strings — If you ’ re performing repeated modifi cations on a string, for example,<br />

to build up a lengthy string prior to displaying it or passing it to some other method or<br />

application, the String class can be very ineffi cient. When you fi nd yourself in this kind of<br />

situation, another class, System.Text.StringBuilder , is more suitable because it has been<br />

designed exactly for this situation.<br />

Formatting expressions — This chapter takes a closer look at those formatting expressions that<br />

have been used in the Console.WriteLine() method throughout the past few chapters. These<br />

formatting expressions are processed using a couple of useful interfaces, IFormatProvider<br />

<strong>and</strong> IFormattable . By implementing these interfaces on your own classes, you can actually<br />

defi ne your own formatting sequences so that Console.WriteLine() <strong>and</strong> similar classes will<br />

display the values of your classes in whatever way you specify.<br />

Regular expressions — .<strong>NET</strong> also offers some very sophisticated classes that deal with<br />

situations in which you need to identify or extract substrings that satisfy certain fairly<br />

sophisticated criteria; for example, fi nding all occurrences within a string where a character or<br />

set of characters is repeated, fi nding all words that begin with “ s ” <strong>and</strong> contain at least one “ n ,”<br />

or strings that adhere to an employee ID or a Social Security number construction. Although<br />

you can write methods to perform this kind of processing using the String class, such methods<br />

are cumbersome to write. Instead, you will fi nd that some classes, specifi cally those from<br />

System.Text.RegularExpressions , are designed to perform this kind of processing.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!