Chapter 8

Strings and Regular Expressions

One of the most common data types used in programming is the string. In C#, a string is a group of one or more characters declared using the string keyword. Strings play an important part in programming and are an integral part of our lives — our names, addresses, company names, email addresses, web site URLs, flight numbers, and so forth are all made up of strings. To help manipulate those strings and pattern matching, you use regular expressions, sequences of characters that define the patterns of a string. In this chapter, then, you will:

□ Explore the System.String class

□ Learn how to represent special characters in string variables

□ Manipulate strings with various methods

□ Format strings

□ Use the StringBuilder class to create and manipulate strings

□ Use Regular Expressions to match string patterns

The System.String Class

The .NET Framework contains the System.String class for string manipulation. To create an instance of the String class and assign it a string, you can use the following statements:

String str1;

str1 = 'This is a string';

C# also provides an alias to the String class: string (lowercase 's'). The preceding statements can be rewritten as:

string str1; //---equivalent to String str1;---

str1 = 'This is a string';

You can declare a string and assign it a value in one statement, like this:

string str2 = 'This is another string';

In .NET, a string is a reference type but behaves very much like a value type. Consider the following example of a typical reference type:

Button btn1 = new Button() { Text = 'Button 1' };

Button btn2 = btn1;

btn1.Text += ' and 2'; //---btn1.text is now 'Button 1 and 2'---

Console.WriteLine(btn1.Text); //---Button 1 and 2---

Console.WriteLine(btn2.Text); //---Button 1 and 2---

Here, you create an instance of a Button object (btn1) and then assign it to another variable (btn2). Both btn1 and btn2 are now pointing to the same object, and hence when you modify the Text property of btn1, the changes can be seen in btn2 (as is evident in the output of the WriteLine() statements).

Because strings are reference types, you would expect to see the same behavior as exhibited in the preceding block of code. For example:

string str1 = 'String 1';

string str2 = str1;

str1 and str2 should now be pointing to the same instance. Make some changes to str1 by appending some text to it:

str1 += ' and some other stuff';

And then print out the value of these two strings:

Console.WriteLine(str1); //---String 1 and some other stuff---

Console.WriteLine(str2); //---String 1---

Are you surprised to see that the values of the two strings are different? What actually happens when you do the string assignment (string str2 = str1) is that str1 is copied to str2 (str2 holds a copy of str1; it does not points to it). Hence, changes made to str1 are not reflected in str2.

A string cannot be a value type because of its unfixed size. All values types (int, double, and so on) have fixed size.

A string is essentially a collection of Unicode characters. The following statements show how you enumerate a string as a collection of char and print out the individual characters to the console:

string str1 = 'This is a string';

foreach (char c in str1) {

 Console.WriteLine(c);

}

Here's this code's output:

T

h

i

s

i

s

a

s

t

r

i

n

g

Escape Characters

Certain characters have special meaning in strings. For example, strings are always enclosed in double quotation marks, and if you want to use the actual double-quote character in the string, you need to tell the C# compiler by 'escaping' the character's special meaning. For instance, say you need to represent the following in a string:

'I don't necessarily agree with everything I say.' Marshall McLuhan

Because the sentence contains the double-quote characters, simply using a pair of double- quotes to contain it will cause an error:

//---error--- string quotation;

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату