Visual Basic 2005 for Programmers (2nd Edition)
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. None of these methods modifies the original String. Figure 16.8. String methods Replace, ToLower, ToUpper and trim.
Line 18 uses String method Replace to return a new String, replacing every occurrence in string1 of character 'e' with character 'E'. Method Replace takes two argumentsa Char for which to search and another Char with which to replace all matching occurrences of the first argument. This method is also overloaded to receive two String parameters. If there are no occurrences of the first argument in the String, the method returns the original String. String method ToUpper returns a new String (line 22) that replaces any lowercase letters in string1 with their uppercase equivalent. If there are no characters to convert to uppercase, the method returns the original String. Line 23 uses String method ToLower to return a new String in which any uppercase letters in string2 are replaced by their lowercase equivalents. As with ToUpper, if there are no characters to convert to lowercase, method ToLower returns the original String. Line 27 uses String method TRim to remove all whitespace characters that appear at the beginning and end of a String. The method returns a new String that contains the original String, but omits leading or trailing whitespace characters. Another version of method TRim takes a character array and returns a String that does not contain the characters in the array argument at the start or end of the String. Line 29 demonstrates that string1 did not change. |