Introduction

This chapter introduces the FCL's string- and character-processing capabilities, and demonstrates how to use regular expressions to search for patterns in text. The techniques presented in this chapter can be employed in text editors, word processors, page-layout software, computerized typesetting systems and other kinds of text-processing software. Previous chapters presented some basic string-processing capabilities. In this chapter, we discuss in detail the text-processing capabilities of class string and type Char from the System namespace and class StringBuilder from the System.Text namespace.

We begin with an overview of the fundamentals of characters and strings in which we discuss character constants and string literals. We then provide examples of class string's many constructors and methods. The examples demonstrate how to determine the length of strings, copy strings, access individual characters in strings, search strings, obtain substrings from larger strings, compare strings, concatenate strings, replace characters in strings and convert strings to uppercase or lowercase letters.

Next, we introduce class StringBuilder, which is used to build strings dynamically. We demonstrate StringBuilder capabilities for determining and specifying the size of a StringBuilder, as well as appending, inserting, removing and replacing characters in a StringBuilder object. We then introduce the character-testing methods of struct Char that enable a program to determine whether a character is a digit, a letter, a lowercase letter, an uppercase letter, a punctuation mark or a symbol other than a punctuation mark. Such methods are useful for validating individual characters in user input. In addition, type Char provides methods for converting a character to uppercase or lowercase.

The chapter concludes with a discussion of regular expressions. We discuss classes Regex and Match from the System.Text.RegularExpressions namespace as well as the symbols that are used to form regular expressions. We then demonstrate how to find patterns in a string, match entire strings to patterns, replace characters in a string that match a pattern and split strings at delimiters specified as a pattern in a regular expression.

Категории