Section 16.9. Miscellaneous String Methods


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.

  1  ' Fig. 16.8: StringMethods2.vb  2  ' Demonstrating String methods Replace, ToLower, ToUpper, Trim,  3  ' and ToString.  4  5  Module StringMethods2  6     Sub Main()  7        Dim string1 As String = "cheers!"  8        Dim string2 As String = "GOOD BYE "  9        Dim string3 As String = " spaces " 10 11        Console.WriteLine("string1 = """ & string1 & """" & vbCrLf & _ 12           "string2 = """ & string2 & """" & vbCrLf & _ 13           "string3 = """ & string3 & """") 14 15        ' call method Replace 16        Console.WriteLine( _ 17           vbCrLf & "Replacing ""e"" with ""E"" in string1: """ & _ 18           string1.Replace("e"c, "E"c) & """") 19 20        ' call ToLower and ToUpper 21        Console.WriteLine(vbCrLf & "string1.ToUpper() = """ & _ 22           string1.ToUpper() & """" & vbCrLf & "string2.ToLower() = """ & _ 23           string2.ToLower() & """") 24 25        ' call Trim method 26        Console.WriteLine( _ 27           vbCrLf & "string3 after trim = """ & string3.Trim() & """") 28 29        Console.WriteLine(vbCrLf & "string1 = """ & string1 & """") 30     End Sub ' Main 31  End Module ' StringMethods2 

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



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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