Option Compare 语句Option Compare Statement

声明比较字符串数据时要使用的默认比较方法。Declares the default comparison method to use when comparing string data.

语法Syntax

Option Compare { Binary | Text }  

组成部分Parts

术语Term 定义Definition
Binary 可选。Optional. 字符串比较中的结果基于派生自字符的内部二进制表示形式排序顺序。Results in string comparisons based on a sort order derived from the internal binary representations of the characters.

这种比较类型在字符串包含不能解释为文本的字符时尤其有用。This type of comparison is useful especially if the strings can contain characters that are not to be interpreted as text. 在这种情况下,你不想因为字母顺序的等效性(如,不区分大小写)而使比较出现偏差。In this case, you do not want to bias comparisons with alphabetical equivalences, such as case insensitivity.
Text 可选。Optional. 字符串比较中的结果基于由系统的区域设置确定的不区分大小写的文本排序顺序。Results in string comparisons based on a case-insensitive text sort order determined by your system's locale.

如果字符串包含所有文本字符且你想在比较它们时考虑到母顺序等效性(如,不区分大小写和紧密相关的字母),则这种比较类型非常有用。This type of comparison is useful if your strings contain all text characters, and you want to compare them taking into account alphabetic equivalences such as case insensitivity and closely related letters. 例如,你可能认定 A 等于 aÄä 出现在 Bb 之前。For example, you might want to consider A and a to be equal, and Ä and ä to come before B and b.

备注Remarks

使用时,Option Compare 语句必须在文件中任何其他源代码语句之前。If used, the Option Compare statement must appear in a file before any other source code statements.

Option Compare 语句指定字符串比较方法(BinaryText)。The Option Compare statement specifies the string comparison method (Binary or Text). 默认的文本比较方法是 BinaryThe default text comparison method is Binary.

Binary 比较会对每个字符串中每个字符的数字 Unicode 值进行比较。A Binary comparison compares the numeric Unicode value of each character in each string. Text 比较会基于当前区域性中的词汇意义对每个 Unicode 字符进行比较。A Text comparison compares each Unicode character based on its lexical meaning in the current culture.

在 Microsoft Windows 中,排序顺序取决于代码页。In Microsoft Windows, sort order is determined by the code page. 有关详细信息,请参阅代码页For more information, see Code Pages.

在下例中,使用 Option Compare Binary 对英语/欧洲代码页 (ANSI 1252) 中的字符进行排序,由此产生典型的二进制排序顺序。In the following example, characters in the English/European code page (ANSI 1252) are sorted by using Option Compare Binary, which produces a typical binary sort order.

A < B < E < Z < a < b < e < z < À < Ê < Ø < à < ê < ø

使用 Option Compare Text 对同一代码页中的相同字符进行排序时,会产生以下文本排序顺序。When the same characters in the same code page are sorted by using Option Compare Text, the following text sort order is produced.

(A=a) < (À = à) < (B=b) < (E=e) < (Ê = ê) < (Z=z) < (Ø = ø)

当 Option Compare 语句不存在时When an Option Compare Statement Is Not Present

如果源代码不包含 Option Compare 语句,则使用 "编译" 页上的 " 选项比较 " 设置 ,"项目设计器" (Visual Basic) If the source code does not contain an Option Compare statement, the Option Compare setting on the Compile Page, Project Designer (Visual Basic) is used. 如果使用命令行编译器,则使用 -optioncompare 编译器选项指定的设置。If you use the command-line compiler, the setting specified by the -optioncompare compiler option is used.

备注

以下说明中的某些 Visual Studio 用户界面元素在计算机上出现的名称或位置可能会不同。Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. 这些元素取决于你所使用的 Visual Studio 版本和你所使用的设置。The Visual Studio edition that you have and the settings that you use determine these elements. 有关详细信息,请参阅个性化设置 IDEFor more information, see Personalizing the IDE.

若要在 IDE 中设置 Option CompareTo set Option Compare in the IDE

  1. 解决方案资源管理器中,选择一个项目。In Solution Explorer, select a project. 在“项目”菜单上,单击“属性” 。On the Project menu, click Properties.

  2. 单击“编译”选项卡。Click the Compile tab.

  3. 设置 " 选项比较 " 框中的值。Set the value in the Option Compare box.

创建项目时,"编译" 选项卡上的 "选项比较" 设置设置为 "选项" 对话框中的 " option compare " 设置。When you create a project, the Option Compare setting on the Compile tab is set to the Option Compare setting in the Options dialog box. 若要更改此设置,请在 " 工具 " 菜单上单击 " 选项"。To change this setting, on the Tools menu, click Options. 在“选项”对话框中,展开“项目和解决方案”,然后单击“VB 默认值”************。In the Options dialog box, expand Projects and Solutions, and then click VB Defaults. VB 默认设置中的初始默认设置为 "二进制"。The initial default setting in VB Defaults is Binary.

若要设置命令行上的 Option CompareTo set Option Compare on the command line

示例Example

下例使用 Option Compare 语句将二进制比较设置为默认字符串比较方法。The following example uses the Option Compare statement to set the binary comparison as the default string comparison method. 若要使用此代码,请取消 Option Compare Binary 语句的注释,并将其置于源文件顶部。To use this code, uncomment the Option Compare Binary statement, and put it at the top of the source file.

' Option Compare Binary

Console.WriteLine("A" < "a")
' Output: True

示例Example

下例使用 Option Compare 语句将不区分大小写的文本排序顺序设置为默认字符串比较方法。The following example uses the Option Compare statement to set the case-insensitive text sort order as the default string comparison method. 若要使用此代码,请取消 Option Compare Text 语句的注释,并将其置于源文件顶部。To use this code, uncomment the Option Compare Text statement, and put it at the top of the source file.

' Option Compare Text

Console.WriteLine("A" = "a")
' Output: True

另请参阅See also