Section 15.1. Creating Strings


15.1. Creating Strings

C# treats strings as if 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 (and if no longer used, is collected by the Garbage Collector).

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.

15.1.1. 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.

15.1.2. 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 \n do not mean print a slash and then an n, but rather mean print a new-line). You indicate escape characters by preceding a letter or punctuation mark with a backslash ( \ ). The two most common escape characters are \n , which is used to create a new line, and \t , which is used to insert a tab into a string. If you need to include a quotation mark ( " ) within a string, you indicate that this is in 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" 

15.1.3. 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 \books"; 

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. A second example illustrates two ways to specify multiline verbatim strings . The first definition uses a non-verbatim string with a newline escape character ( \n ) to signal the line break. The second definition uses a verbatim string literal:

 string s3 = "Line One\nLine 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.

15.1.4. 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 .



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net