Option Compare (statement)

Syntax

Option Compare [Binary | Text]

Description

Controls how strings are compared.

Comments

When Option Compare is set to Binary, then string comparisons are case-sensitive (for example, "A" does not equal "a"). When it is set to Text, string comparisons are case-insensitive (for example, "A" is equal to "a").

The default value for Option Compare is Binary.

The Option Compare statement affects all string comparisons in any statements that follow the Option Compare statement. Additionally, the setting affects the default behavior of Instr, StrComp, and the Like operator. The following table shows the types of string comparisons affected by this setting:

> < <>
<= >= Instr
StrComp Like

The Option Compare statement must appear outside the scope of all subroutines and functions. In other words, it cannot appear within a Sub or Function block.

Example

This example shows the use of Option Compare.

Option Compare Binary
Sub CompareBinary
  a$ = "This String Contains UPPERCASE."
  b$ = "this string contains uppercase."
  If a$ = b$ Then
    MsgBox "The two strings were compared case-insensitive."
  Else
    MsgBox "The two strings were compared case-sensitive."
  End If
End Sub

Option Compare Text
Sub CompareText
  a$ = "This String Contains UPPERCASE."
  b$ = "this string contains uppercase."
  If a$ = b$ Then
    MsgBox "The two strings were compared case-insensitive."
  Else
    MsgBox "The two strings were compared case-sensitive."
  End If
End Sub

Sub Main()
 CompareBinary        'Calls subroutine above.
 CompareText          'Calls subroutine above.
End Sub

See Also

Like (operator); InStr (function); StrComp (function); Comparison Operators (topic).

More information

O