Visual Basic Language Reference  

Option Compare Statement

Used at file level to declare the default comparison method to use when string data is compared.

Option Compare { Binary | Text }

Parts

Binary
Optional. Results in string comparisons based on a sort order derived from the internal binary representations of the characters.
Text
Optional. Results in string comparisons based on a case-insensitive text sort order determined by your system's locale.

Remarks

If used, the Option Compare statement must appear in a file before any other source statements.

The Option Compare statement specifies the string comparison method (Binary or Text) for a class, module or structure. If an Option Compare statement is not included, the default text comparison method is Binary.

In Microsoft Windows, sort order is determined by the code page. In the following example, characters are sorted using Option Compare Binary, which produces a typical binary sort order:

A < B < E < Z < a < b < e < z < ? < ? < ? < ? < ? < ?

When the same characters are sorted using Option Compare Text, the following text sort order is produced:

(A=a) < ( ?=?) < (B=b) < (E=e) < (?=?) < (Z=z) < (?=?)

Example

This example uses the Option Compare statement to set the default string comparison method. The Option Compare statement is used at the module level only.

' Set the string comparison method to Binary.
Option Compare Binary   ' That is, "AAA" is less than "aaa".
' Set the string comparison method to Text.
Option Compare Text   ' That is, "AAA" is equal to "aaa".

See Also

Comparison Operators | InStr Function | InStrRev Function | Option Strict Statement | Option Explicit Statement | Replace Function | Split Function | StrComp Function | /optionexplicit | /optionstrict | /optioncompare:binary | /optioncompare:text