Working with Strings

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

METHOD

PURPOSE

Compare

Compares two given String objects.

Concat

Concatenates one or more strings.

Copy

Copies the current string.

CopyTo

Copies a given number of characters from a given position in this string to a given position in an array of Unicode characters.

EndsWith

Determines whether the end of this string matches the given string.

IndexOf

Returns the 0-based index of the first occurrence of a string, or one or more characters within this string.

IndexOfAny

Returns the 0-based index of the first occurrence in this string of any character in a specified array of Unicode characters.

Insert

Inserts a given string at a given index position in this string.

Join

Concatenates a given separator string between each element of a given string array, resulting in a single concatenated string.

LastIndexOf

Reports the index position of the last occurrence of a given Unicode character or string within this string.

LastIndexOfAny

Reports the index position of the last occurrence in this string of one or more characters given in a Unicode array.

PadLeft

Right-aligns the characters in this string, padding the left with spaces or a given Unicode character.

PadRight

Left-aligns the characters in this string, padding the right with spaces or a given Unicode character.

Remove

Deletes a given number of characters from this string.

Replace

Replaces all occurrences of a given Unicode character or string in this string with another given Unicode character or string.

Split

Identifies the substrings in this string that are delimited by one or more characters given in an array, and stores the substrings into a string array.

StartsWith

Determines whether the beginning of this string matches the given string.

Substring

Returns a substring from this string.

ToCharArray

Copies the characters in this string to a Unicode character array.

ToLower

Returns this string in lowercase.

ToUpper

Returns this string in uppercase.

Trim

Removes all occurrences of a set of characters you specify from the beginning and end of this string.

TrimEnd

Removes all occurrences of a set of characters in an array from the end of this string.

TrimStart

Removes all occurrences of a set of characters in an array from the beginning of this string.

As in other languages, you can also embed escape sequences , which start with a backslash, \ , in strings. These escape sequences have these special meanings:

  • \ordinary characters are characters other than . $ ^ { [ ( ) * + ? \ that stand for themselves .

  • \a is a bell (alarm).

  • \b is a backspace .

  • \t is a tab.

  • \r is a carriage return.

  • \v is a vertical tab.

  • \f is a form feed.

  • \n is a new line.

  • \e is an escape.

  • \0 XX is an ASCII character as octal (up to three digits).

  • \xXX is an ASCII character using hexadecimal representation (exactly two digits).

  • \c X is an ASCII control character (for example, \cC is ^C ).

  • \u XXXX is a Unicode character using hexadecimal representation (exactly four digits).

  • \ , when followed by a character that is not recognized as an escaped character, is the same as that character.

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

FOR C++ PROGRAMMERS

In C#, the prefix @ in front of a string turns off escaping.


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.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

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