Chapter 9. Strings and Regular Expressions


  • Building strings

  • Formatting expressions

  • Using regular expressions

Since the beginning of this book, you have been using strings almost constantly, and might not have realized that the stated mapping that the string keyword in C# actually refers to is the System.String .NET base class. System.String is a very powerful and versatile class, but it is by no means the only string-related class in the .NET armory. This chapter starts by reviewing the features of System.String and then looks at some nifty things you can do with strings using some of the other .NET classes — in particular those in the System.Text and System.Text.RegularExpressions namespaces. This chapter covers the following areas:

  • Building strings — If you're performing repeated modifications on a string, for example, to build up a lengthy string prior to displaying it or passing it to some other method or application, the String class can be very inefficient. When you find yourself in this kind of situation, another class, System.Text.StringBuilder, is more suitable because it has been designed exactly for this situation.

  • Formatting expressions — This chapter takes a closer look at those formatting expressions that have been used in the Console.WriteLine() method throughout the past few chapters. These formatting expressions are processed using a couple of useful interfaces, IFormatProvider and IFormattable. By implementing these interfaces on your own classes, ...

Get Professional C# 4 and .NET 4 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.