Strings in C# correspond to the .NET String type, and there's a great deal of functionality built into them. For example, you can append one string to another with the + operator: string string1 = "Hello "; string string2 = "there!"; string2 = string1 + string2; This code leaves "Hello there!" in string2 . You can also access the individual characters as if the string were an array, like this: string string1 = "Hello "; char char1 = string1[1]; This code leaves 'e' in char1 (note that, as in C++, character literals are enclosed in single quotes, like 'e' , whereas string quotes are enclosed in double quotes, like "Hello!" ). Strings come with many built-in methods as well. For example, take a look at the code in ch02_23.cs, Listing 2.23, which determines the length of a string and uses the IndexOf method to find the location of a substring in that string. Listing 2.23 Using String Methods (ch02_23.cs)class ch02_23 { static void Main(string[] args) { string now = "Now is the time."; System.Console.WriteLine("In the string '{0}', which is " + "{1} characters long, 'the' begins at position {2}.", now, now.Length, now.IndexOf("the")); } } Here's what you see when you run this code: C:\>ch02_23 In the string 'Now is the time.', which is 16 characters long, 'the' begins at position 7. You can find a summary of the more significant string methods in Table 2.2. All these methods can be applied directly to strings in C#. Table 2.2. Significant String Methods
As in other languages, you can also embed escape sequences , which start with a backslash, \ , in strings. These escape sequences have these special meanings:
For example, the string "Now\tis\tthe\ttime." is interpreted to have three tab characters in it (and will appear that way if you display it with Console.WriteLine ). And you can embed double quotation marks in string by escaping them like this: "He said, \"Now is the time.\"" . If you display this string with Console.WriteLine , you'll see He said, "Now is the time." Sometimes, however, escape characters get in the way, as when you want to use the string "c:\csharp\folder1\timer" . In this case, you want the backslashes to stand for directory separators, not escape characters. You can escape each backslash to preserve it as a backslash, "c:\\csharp\\folder1\\timer" , or can turn off escaping in C# by prefacing a string literal with an ampersand, @ , like this: @"c:\csharp\folder1\timer" . You can see another example using string methods in ch02_24.cs, Listing 2.24; in this example, we're using the string Insert method to insert "not " into the string "Now is the time." , giving us "Now is not the time." .
Listing 2.24 Using the Insert Method (ch02_24.cs) class ch02_24 { static void Main(string[] args) { string now = "Now is the time."; now = now.Insert(7, "not "); System.Console.WriteLine(now); } } Here's the result of ch02_24: C:\>ch02_24 Now is not the time. Besides the methods you see in Table 2.2, C# strings also enable you to work with regular expressions , which C++ does not. |