Visual Basic 2005 for Programmers (2nd Edition)
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.
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.
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. |