16.9 Miscellaneous string Methods

Class string provides several methods that return modified copies of strings. Figure 16.8 demonstrates string methods Replace, ToLower, ToUpper and Trim.

Fig. 16.8 Demonstrating string methods Replace, ToLower, ToUpper and Trim.

Alternate View

 1    // Fig. 16.8: StringMethods2.cs
 2    // Demonstrating string methods Replace, ToLower, ToUpper and Trim
 3
 4    using System;
 5
 6    class StringMethods2
 7    {
 8       static void Main()
 9       {
10          var string1 = "cheers!";
11          var string2 = "GOOD BYE ";
12          var string3 = "   spaces   ";
13
14          Console.WriteLine($"string1 = "{string1}"
" +
15             $"string2 = "{string2}"
" +
16             $"string3 = "{string3}"");
17
18          // call method Replace
19          Console.WriteLine("
Replacing "e" with "E" in string1: " +
20             $""{string1.Replace('e', 'E')}"");
21
22          // call ToLower and ToUpper
23          Console.WriteLine(
24             $"
string1.ToUpper() = "{string1.ToUpper()}"" +
25             $"
string2.ToLower() = "{string2.ToLower()}"");
26
27          // call Trim method
28          Console.WriteLine(
29             $"
string3 after trim = "{string3.Trim()}"");
30
31          Console.WriteLine($"
string1 = "{string1}"");
32       }
33    }

string1 = "cheers!"
string2 = "GOOD BYE "
string3 = "   spaces   "

Replacing "e" with "E" in string1: "chEErs!"

string1.ToUpper() = "CHEERS!"
string2.ToLower() = "good bye "

string3 after trim = "spaces"

string1 = "cheers!"

Line 20 uses string method Replace to return a new string, replacing every occur-rence in string1 of character 'e' with 'E'. Method Replace takes two arguments—a char for which to search and another char with which to replace all matching occurrences of the first argument. The original string remains unchanged. If there are no occurrences of the first argument in the string, the method returns the original string. An overloaded version of this method allows you to provide two strings as arguments.

The string method ToUpper generates a new string (line 24) that replaces any lower-case letters in string1 with their uppercase equivalents (using the current culture’s rules). The method returns a new string containing the converted string; the original string remains unchanged. If there are no characters to convert, the original string is returned. Line 25 uses string method ToLower to return a new string in which any uppercase letters in string2 are replaced by their lowercase equivalents (using the current culture’s rules). The original string is unchanged. As with ToUpper, if there are no characters to convert to lowercase, method ToLower returns the original string.

Line 29 uses string method Trim to remove all whitespace characters that appear at the beginning and end of a string. Without otherwise altering the original string, the method returns a new string that contains the string, but omits leading and trailing whitespace characters. This method is particularly useful for retrieving user input (i.e., via a TextBox). Another version of method Trim takes a character array and returns a copy of the string that does not begin or end with any of the characters in the array argument.

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

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