Table of contents
TOC
折叠目录
展开目录

StrComp 函数StrComp Function

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

返回一个variant 类型的值整数),指示字符串比较的结果。语法StrCompstring1string2 [、比较])StrComp函数语法具有下面这些命名参数Returns a Variant ( Integer ) indicating the result of astring comparison. SyntaxStrComp ( string1, string2 [, compare ]) The StrComp function syntax has thesenamed arguments:

部件Part说明Description
string1string1必填。任何有效的字符串表达式Required. Any valid string expression.
string2string2必需。任何有效的字符串表达式。Required. Any valid string expression.
比较compare可选。指定字符串比较的类型。如果比较参数Null,将发生错误。如果省略比较比较选项设置用于确定比较的类型。Optional. Specifies the type of string comparison. If the compareargument isNull, an error occurs. If compare is omitted, the Option Compare setting determines the type of comparison.

设置比较操作参数的设置是:Settings The compare argument settings are:

常量ConstantValue说明Description
vbUseCompareOptionvbUseCompareOption-1-1通过使用该选项比较语句设置比较。Performs a comparison using the setting of the Option Compare statement.
vbBinaryComparevbBinaryCompare00执行二进制比较。Performs a binary comparison.
vbTextComparevbTextCompare11执行文本比较。Performs a textual comparison.
vbDatabaseComparevbDatabaseCompare22仅适用于 Microsoft Access。根据数据库中的信息执行比较。Microsoft Access only. Performs a comparison based on information in your database.

返回值StrComp函数具有下列返回值:Return Values The StrComp function has the following return values:

如果IfStrComp 返回StrComp returns
string1小于string2string1 is less than string2-1-1
string1是等于string2string1 is equal to string200
string1是大于string2string1 is greater than string211
string1string2string1 or string2 is Null空值Null

示例Example

本示例使用StrComp函数返回字符串的比较结果。如果第三个参数为 1,则执行文本比较;如果第三个参数为 0 或省略,则执行二进制比较。This example uses the StrComp function to return the results of a string comparison. If the third argument is 1, a textual comparison is performed; if the third argument is 0 or omitted, a binary comparison is performed.

Dim MyStr1, MyStr2, MyComp
MyStr1 = "ABCD": MyStr2 = "abcd"    ' Define variables.
MyComp = StrComp(MyStr1, MyStr2, 1)    ' Returns 0.
MyComp = StrComp(MyStr1, MyStr2, 0)    ' Returns -1.
MyComp = StrComp(MyStr2, MyStr1)    ' Returns 1.

© 2018 Microsoft