Strings and Regular Expressions C# Help

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 .NETbase 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. Regular Expressions namespaces. This chapter covers the following areas:

  1.  Building strings – If you’re performing repeated modifications on a string, for example, in order 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. For this kind of situation, another class, System. Text. String Builder, is more suitable because it has been designed exactly for this situation.
  2.  Formatting expressions – We also take 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, IFormat Provider and IFormattable. By implementing these interfaces on your own classes, you can actually define your own formatting sequences so that Console. WriteLine () and similar classes will display the values of your classes  whatever way you specify.
  3. Regular expressions – .NET also offers some very sophisticated classes that deal with situations in which you need to identify or extract substrings that satisfy certain fairly sophisticated criteria; for example, finding all occurrences within a string where a character or set of characters is repeated, finding all words that begin with s and contain at least one n, or strings that adhere to employee lD or Social Security number constructions. Although you can write methods to perform this kind of processing using the String class, such methods are cumbersome to write. Instead, you can use some classes from System. Text. Regular Expressions, which are designed specifically to perform this kind of processing .




Posted on October 29, 2015 in Strings and Regular Expressions

Share the Story

Back to Top