Section 16.5. Comparing Strings


16.5. Comparing Strings

The next two examples demonstrate the various methods for comparing Strings. To understand how one String can be "greater than" or "less than" another String, consider the process of alphabetizing a series of last names. The reader would, no doubt, place "Jones" before "Smith", because the first letter of "Jones" comes before the first letter of "Smith" in the alphabet. The alphabet is more than just a set of 26 lettersit is an ordered list of characters in which each letter occurs in a specific position. For example, Z is more than just a letter of the alphabet; Z is specifically the 26th letter of the alphabet.

Computers can order characters alphabetically because the characters are represented internally as Unicode numeric codes. When comparing two Strings, the string comparison methods simply compare the numeric codes of the characters in the Strings.

Comparing Strings with = and String Methods Equals and CompareTo

Class String provides several ways to compare Strings. Figure 16.3 demonstrates method Equals, method CompareTo and the equality operator (=).

Figure 16.3. String test to determine equality.

  1  ' Fig. 16.3: StringCompare.vb  2  ' Comparing Strings  3  4  Module StringCompare  5     Sub Main()  6        Dim string1 As String = "hello"  7        Dim string2 As String = "good bye"  8        Dim string3 As String = "Happy Birthday"  9        Dim string4 As String = "happy birthday" 10 11        ' output values of four strings 12        Console.WriteLine("string1 = """ & string1 & """" & vbCrLf & _ 13           "string2 = """ & string2 & """" & vbCrLf & _ 14           "string3 = """ & string3 & """" & vbCrLf & _ 15           "string4 = """ & string4 & "" & vbCrLf) 16 17        ' test for equality using Equals method 18        If string1.Equals("hello") Then 19           Console.WriteLine("string1 equals ""hello""") 20        Else 21          Console.WriteLine("string1 does not equal ""hello""") 22        End If 23 24        ' test for equality with = 25        If string1 = "hello" Then 26           Console.WriteLine("string1 equals ""hello""") 27        Else 28           Console.WriteLine("string1 does not equal ""hello""") 29        End If 30 31        ' test for equality comparing case 32        If String.Equals(string3, string4) Then ' static method 33           Console.WriteLine("string3 equals string4") 34        Else 35           Console.WriteLine("string3 does not equal string4") 36        End If 37 38        ' test CompareTo 39        Console.WriteLine(vbCrLf & "string1.CompareTo( string2 ) is " & _ 40           string1.CompareTo(string2) & vbCrLf & _ 41           "string2.CompareTo(string1) is " & _ 42           string2.CompareTo(string1) & vbCrLf & _ 43           "string1.CompareTo(string1) is " & _ 44           string1.CompareTo(string1) & vbCrLf & _ 45           "string3.CompareTo(string4) is " & _ 46           string3.CompareTo(string4) & vbCrLf & _ 47           "string4.CompareTo(string3) is " & _ 48           string4.CompareTo(string3) & vbCrLf) 49     End Sub ' Main 50  End Module ' StringCompare 

 string1 = "hello" string2 = "good bye" string3 = "Happy Birthday" string4 = "happy birthday" string1 equals "hello" string1 equals "hello" string3 does not equal string4 string1.CompareTo( string2 ) is 1 string2.CompareTo( string1 ) is -1 string1.CompareTo( string1 ) is 0 string3.CompareTo( string4 ) is 1 string4.CompareTo( string3 ) is -1 



The condition in the If statement (line 18) uses String method Equals to compare string1 and the String literal "hello" to determine whether they are equal. Method Equals (inherited by String from class Object) tests any two objects for equality (i.e., whether the objects have the same contents). The method returns TRue if the objects are equal and False otherwise. In this instance, the preceding condition returns TRue because string1 references the String literal object "hello". Method Equals uses a lexicographical comparisonthe integer Unicode values that represent each character in each String are compared. Comparing "hello" with "HELLO" would return False because the numeric representations of lowercase letters are different from the numeric representations of corresponding uppercase letters.

The condition in the second If statement (line 25) uses the equality operator (=) to compare string1 with the String literal "hello" for equality. In Visual Basic, the equality operator also uses a lexicographical comparison to compare two Strings. Thus, the condition in the If statement evaluates to TRue because the values of string1 and "hello" are equal.

Line 32 compares string3 and string4 for equality to illustrate that comparisons are indeed case sensitive. Shared method Equals is used to compare the values of two Strings. "Happy Birthday" does not equal "happy birthday", so the condition of the If statement fails, and the message "string3 does not equal string4" is output (line 35).

Lines 3948 use String method CompareTo to compare Strings. Method CompareTo returns 0 if the Strings are equal, -1 if the String that invokes CompareTo is less than the String passed as an argument and 1 if the String that invokes CompareTo is greater than the String passed as an argument. Method CompareTo uses a lexicographical comparison.

Note that CompareTo considers string3 to be larger than string4. The only difference between these two Strings is that string3 contains two uppercase letters in positions where string4 contains lowercase letters.

Using String Methods StartsWith and EndsWith

The application in Fig. 16.4 shows how to test whether a String begins or ends with a given String. Method StartsWith determines whether a String starts with the String passed to it as an argument. Method EndsWith determines whether a String ends with the String passed to it as an argument. Class StringStartEnd's Main method defines an array of Strings (called strings), which contains "started", "starting", "ended" and "ending". The remainder of method Main tests the elements of the array to determine whether they start or end with a particular set of characters.

Figure 16.4. StartsWith and EndsWith methods.

  1  ' Fig. 16.4: StringStartEnd.vb  2  ' Demonstrating StartsWith and EndsWith methods.  3  4  Module StringStartEnd  5     Sub Main()  6        Dim strings As String() = _  7           {"started", "starting", "ended", "ending"}  8  9        ' test every string to see if it starts with "st" 10        For i As Integer = 0 To strings.Length - 1 11           If strings(i).StartsWith("st") Then 12              Console.WriteLine( _ 13                 """" & strings(i) & """" & " starts with ""st""") 14           End If 15        Next i 16 17        Console.WriteLine("") 18 19        ' test every string to see if it ends with "ed" 20        For i As Integer = 0 To strings.Length - 1 21           If strings(i).EndsWith("ed") Then 22              Console.WriteLine( _ 23                 """" & strings(i) & """" & " ends with ""ed""") 24           End If 25        Next i 26 27        Console.WriteLine("") 28     End Sub ' Main 29  End Module ' StringStartEnd 

 "started" starts with "st" "starting" starts with "st" "started" ends with "ed" "ended" ends with "ed" 



Line 11 uses method Starts With, which takes a String argument. The condition in the If statement determines whether the String at index i of the array starts with the characters "st". If so, the method returns true, and lines 1213 output a message.

Line 21 uses method Ends With, which also takes a String argument. The condition in the If statement determines whether the String at index i of the array ends with the characters "ed". If so, the method returns true, and lines 2223 output a message.



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