In C#, strings can also be @-quoted. Earlier, you saw that to include special characters (such as double- quote, backslash, and so on) in a string you need to use the backslash character to turn off its special meaning:
string path='C:\Windows';
You can actually use the @
character, and prefix the string with it, like this:
string path=@'C:Windows';
Using the @
character makes your string easier to read. Basically, the compiler treats strings that are prefixed with the @ character verbatim — that is, it just accepts all the characters in the string (inside the quotes). To better appreciate this, consider the following example where a string containing an XML snippet is split across multiple lines (with each line ending with a carriage return):
string XML = @'
<Books>
<title>C# 3.0 Programmers' Reference</title>
</Book>';
Console.WriteLine(XML);
Figure 8-7 shows the output. The WriteLine()
method prints out the line verbatim.

Figure 8-7
To illustrate the use of the @ character on a double-quoted string, the following:
string quotation =
''I don't necessarily agree with everything I say.' Marshall McLuhan';
Console.WriteLine(quotation);
can be rewritten as:
string quotation =
@'''I don't necessarily agree with everything I say.'' Marshall McLuhan';
Console.WriteLine(quotation);
C# supports the use of escape code to represent Unicode characters. The four-digit escape code format is: udddd
. For example, the following statement prints out the £ symbol:
string symbol = 'u00A3';
Console.WriteLine(symbol);
For more information on Unicode, check out http://unicode.org/Public/UNIDATA/NamesList.txt.
String Manipulations
Often, once your values are stored in string variables, you need to perform a wide variety of operations on them, such as comparing the values of two strings, inserting and deleting strings from an existing string, concatenating multiple strings, and so on. The String
class in the .NET Framework provides a host of methods for manipulating strings, some of the important ones of which are explained in the following sections.
You can find out about all of the String
class methods at www.msdn.com.
Even though string is a reference type, you will use the ==
and !=
operators to compare the value of two strings (not their references).
Consider the following three string variables:
string str1 = 'This is a string';
string str2 = 'This is a ';
str2 += 'string';
string str3 = str2;
The following statements test the equality of the values contained in each variable:
Console.WriteLine(str1 == str2); //---True---
Console.WriteLine(str1 == str3); //---True---
Console.WriteLine(str2 != str3); //---False---
As you can see from the output of these statements, the values of each three variables are identical. However, to compare their reference equality, you need to cast each variable to object and then check their equality using the == operator, as the following shows:
Console.WriteLine((object)str1 == (object)str2); //---False---
Console.WriteLine((object)str2 == (object)str3); //---True---
However, if after the assignment the original value of the string is changed, the two strings' references will no longer be considered equal, as the following shows:
string str3 = str2;
Console.WriteLine((object)str2 == (object)str3); //---True---
str2 = 'This string has changed';
Console.WriteLine((object)str2 == (object)str3); //---False---
Besides using the ==
operator to test for value equality, you can also use the Equals()
method, which is available as an instance method as well as a static method:
Console.WriteLine(str1 == str2); //---True---
Console.WriteLine(str1.Equals(str2)); //---True---
Console.WriteLine(string.Equals(str1,str2)); //---True---
String comparison is a common operation often performed on strings. Consider the following two string variables:
string str1 = 'Microsoft';
string str2 = 'microsoft';
You can use the String.Compare()
static method to compare two strings:
Console.WriteLine(string.Compare(str1, str2)); // 1;str1 is greater than str2
Console.WriteLine(string.Compare(str2, str1)); // -1;str2 is less than str1
Console.WriteLine(string.Compare(str1, str2, true)); // 0;str1 equals str2
The lowercase character 'm' comes before the capital 'M,' and hence str1
is considered greater than str2
. The third statement compares the two strings without considering the casing (that is, case-insensitive; it's the third argument that indicates that the comparison should ignore the casing of the strings involved).
The String.Compare()
static method is overloaded, and besides the two overloaded methods (first two statements and the third statement) just shown, there are additional overloaded methods as described in the following table.
Method | Description |
---|---|
Compare(String, String) |