Creating Strings

C# treats strings as though they were built-in types (much as it does with arrays). C# strings are flexible, powerful, and easy to use.

In .NET, each string object is an immutable sequence of Unicode characters. In other words, methods that appear to change the string actually return a modified copy; the original string remains intact.

The declaration of the System.String class is (in part):

public sealed class String :
 IComparable, ICloneable, IConvertible, IEnumerable

This declaration reveals that the class is sealed, meaning that it is not possible to derive from the String class. The class also implements four system interfaces—IComparable, ICloneable, IConvertible, and IEnumerable—which dictate functionality that System.String shares with other classes in the .NET Framework: the ability to be sorted, copied, converted to other types, and enumerated in foreach loops, respectively.

String Literals

The most common way to create a string is to assign a quoted string of characters, known as a string literal, to a user-defined variable of type string. The following code declares a string called newString that contains the phrase “This book teaches C#”:

string newString = "This book teaches C#";

To be precise, newString is a string object that is initialized with the string literal "This book teaches C#". If you pass newString to the WriteLine method of the Console object, the string “This book teaches C#” will be displayed.

Escape Characters

Quoted strings can include escape characters (often referred to as “escape sequences”). Escape characters are a way to signal that the letters or characters that follow have a special meaning (for example, the two characters do not mean print a slash and then an n, but rather print a newline). You indicate escape characters by preceding a letter or punctuation mark with a backslash (). The two most common escape characters are , which is used to create a newline, and , which is used to insert a tab into a string. If you need to include a quotation mark (") within a string, you indicate that the quote mark is part of the string (rather than ending the string) by escaping it:

Console.Writeline("This "string" has quotes around it");

This will produce the output: This "string" has quotes around it.

If you want to display the backslash character itself, you must escape it with (you guessed it) another backslash. Thus, if you were writing the string c:myDirectory, you’d write:

"c:\myDirectory"

Verbatim Strings

Strings can also be created using verbatim string literals, which start with the “at” (@) symbol. This tells the String constructor that the string should be used as is (verbatim), even if it spans multiple lines or includes escape characters. In a verbatim string literal, backslashes and the characters that follow them are simply considered additional characters of the string. Thus, the following two definitions are equivalent:

string s1 = "My 'favorite' book is in the directory \books";
string s2 = @"My 'favorite' book is in the directory ooks";

In s1, a nonverbatim string literal is used, and so the quote and backslash characters must be escaped (preceded by a backslash character). The verbatim string s2 does not require the escape characters.

The following example illustrates two ways to specify multiline verbatim strings. The first definition uses a nonverbatim string with a newline escape character ( ) to signal the line break. The second definition uses a verbatim string literal:

string s3 = "Line One
Line Two";
string s4 = @"Line One
Line Two";

If you want to use quotation marks in a verbatim string literal, you use two quotation marks, like this:

string s5 = @"This string has ""quotation marks"" in it.";

Again, these declarations are interchangeable. Which one you use is a matter of convenience and personal style.

The ToString( ) Method

Another common way to create a string is to call the ToString( ) method on an object and assign the result to a string variable. All the built-in types override this method to simplify the task of converting a value (often a numeric value) to a string representation of that value. In the following example, the ToString( ) method of an integer type is called to store its value in a string:

int myInteger = 5;
string integerString = myInteger.ToString( );

The call to myInteger.ToString( ) returns a string object that is then assigned to the string variable, integerString.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset