□ 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 .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
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;
quotation = ""I don't necessarily agree with everything I say." Marshall McLuhan";
To represent the double-quote character in a string, you use the backslash (\) character to turn off its special meanings, like this:
string quotation =
"\"I don't necessarily agree with everything I say.\" Marshall McLuhan";
Console.WriteLine(quotation);
The output is shown in Figure 8-1.
Figure 8-1
A backslash, then, is another special character. To represent the C:\Windows path, for example, you need to turn off the special meaning of \ by using another \ , like this:
string path = "C:\\Windows";
What if you really need two backslash characters in your string, as in the following?
"\\servername\path"
In that case, you use the backslash character twice, once for each of the backslash characters you want to turn off, like this:
string UNC = "\\\\servername\\path";
In addition to using the \
character to turn off the special meaning of characters like the double-quote (") and backslash (\), there are other escape characters that you can use in strings.
One common escape character is the \n
. Here's an example:
string lines = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5";
Console.WriteLine(lines);
The \n
escape character creates a newline, as Figure 8-2 shows.
Figure 8-2
You can also use \t
to insert tabs into your string, as the following example shows (see also Figure 8-3):
string columns1 = "Column 1\tColumn 2\tColumn 3\tColumn 4";
string columns2 = "1\t5\t25\t125";
Console.WriteLine(columns1);
Console.WriteLine(columns2);
Figure 8-3
You learn more about formatting options in the section "String Formatting" later in this chapter.
Besides the \n
and \t
escape characters, C# also supports the \r
escape character. \r
is the carriage return character. Consider the following example:
string str1 = " One";
string str2 = "Two";
Console.Write(str1);
Console.Write(str2);
The output is shown in Figure 8-4.
Figure 8-4
However, if you prefix a \r
escape character to the beginning of str2
, the effect will be different:
string str1 = " One";
string str2 = "\rTwo";
Console.Write(str1);
Console.Write(str2);
The output is shown in Figure 8-5.
Figure 8-5
Читать дальше