Comparing strings
String comparisons are usually done using the normal comparison operators:
| < |
(less than) |
| <= |
(less than or equal to) |
| = |
(equal to) |
| <> |
(not equal to) |
| >= |
(greater than or equal to) |
| > |
(greater than) |
Visual Basic compares strings by comparing each character in the strings.
For example, if the two strings "ABCD123" and "ABCE123" were being compared, Visual Basic would compare the two "A"s, then the two "B"s, then the two "C"s and finally the "D" and "E". Since "D" does not equal "E", the comparison would stop. Since the character code for "D" (68) is less than the character code for "E" (69), the first string ("ABCD123") is said to be less than the second ("ABCE123").
In the case where a short string matches all the characters on the left side of a longer string (such as "AB" and "ABC"), the comparison ends when there are no more characters to test in the shorter string. When this occurs, the shorter string is always considered less than the longer string ("AB" <"abc").
The Option Compare statement effects the evaluation of string comparisons.
Option Compare {Binary | Text}
Modifies the way string comparison are performed. The Binary option performs comparisons in the "usual" manner as described above - comparing each strings' character codes. The Text option causes the comparisons to be case insensitive. That is to say, no distinction is made between upper and lower case letters during comparisons ("A" = "a" and "Z" = "z").
The Option Compare statement must appear before any procedures in a code module and will only effect the comparisons in the module in which it appears. If no Option Compare statement is present in a module, the default comparison option of Binary is used.
StrComp(string1, string2[, compare_mode])
Compares string1 with string2, using a specific comparison mode (compare_mode). If compare_mode is not provided, the current Option Compare mode is used. Since the Option Compare statement effects every string comparison in a module, the StrComp function allows you to override the current Option Compare mode for an individual string comparison.
StrComp returns an integer value to indicate the result of a string comparison.
| Comparison |
Return Value |
| i = StrComp("A", "B") |
-1 (string1 is less than string2) |
| i = StrComp("A", "A") |
0 (string1 is equal to string2) |
| i = StrComp("B", "A") |
1 (string1 is greater than string2) |
Option Compare Binary
i = StrComp("ABC", "abc", vbTextCompare) |
0 |
Option Compare Text
i = StrComp("ABC", "abc", vbBinaryCompare) |
-1 |
Option Compare Text
i = StrComp("abc", "ABC", vbBinaryCompare) |
1 |
string Like pattern
NOTE: This operator is new to Visual Basic, version 6.0.
Performs pattern matching between string and pattern.
Patterns use a special notation. The special characters in pattern that are used to match characters in string are:
| Character in pattern |
Matched value in string |
| ? |
Any single character |
| * |
Zero or more characters |
| # |
Any single digit ("0".."9") |
| [character_list] |
Any single character in character_list |
| [!character_list] |
Any single character not in character_list |
Character lists (character_list) is a special notation for matching any one of a group of characters. Within a character list, you can specify a range of characters by using the hypen (-).
| Range |
Matches |
| [ABC] |
"A" or "B" or "C" |
| [A-C] |
"A" or "B" or "C" |
| [A-Z] |
Any uppercase alphabetic character |
| [A-Za-z] |
Any upper- or lowercase alphabetic character |
Comparisons are done using the current Option Compare mode.
| Comparison |
Results |
| "ABC" Like "A?C" |
True |
| "A3C" Like "A#C" |
True |
| "ABC" Like "A??C" |
False |
| "A1z3C" Like "A*C" |
True |
| "1B3" Like "1[A-Z]3" |
True |
| "123" Like "1[!A-Z]3" |
True |
The Like operator allows you to perform some sophisticated string
comparisons. A simple project using the Like keyword is available here. For further information about pattern matching (including character lists), refer to VB's help facility under "Like operator".
<< Prev] [Next >>