Strings.StrComp(String, String, CompareMethod) 方法


根据字符串的比较结果返回 -1、0 或 1。Returns -1, 0, or 1, based on the result of a string comparison.

public static int StrComp (string String1, string String2, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
static member StrComp : string * string * Microsoft.VisualBasic.CompareMethod -> int
Public Function StrComp (String1 As String, String2 As String, Optional Compare As CompareMethod = Microsoft.VisualBasic.CompareMethod.Binary) As Integer



必需。Required. 任何有效的 String 表达式。Any valid String expression.


必需。Required. 任何有效的 String 表达式。Any valid String expression.


可选。Optional. 指定字符串比较的类型。Specifies the type of string comparison. 如果省略 Compare,则由 Option Compare 设置确定比较类型。If Compare is omitted, the Option Compare setting determines the type of comparison.


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

如果If StrComp 返回StrComp returns
String1 排在 String2 的前面String1 sorts ahead of String2 -1-1
String1 等于 String2String1 is equal to String2 00
String1 排在 String2 的后面String1 sorts after String2 11


Compare 值无效。Compare value is not valid.


此示例使用 StrComp 函数返回字符串比较的结果。This example uses the StrComp function to return the results of a string comparison.

' Defines variables.
Dim testStr1 As String = "ABCD"
Dim testStr2 As String = "abcd"
Dim testComp As Integer
' The two strings sort equally. Returns 0.
testComp = StrComp(testStr1, testStr2, CompareMethod.Text)
' testStr1 sorts before testStr2. Returns -1.
testComp = StrComp(testStr1, testStr2, CompareMethod.Binary)
' testStr2 sorts after testStr1. Returns 1.
testComp = StrComp(testStr2, testStr1, CompareMethod.Binary)


将从第一个字符开始按字母数字排序值对字符串进行比较。The strings are compared by alphanumeric sort values beginning with the first character. 有关二进制比较、文本比较和排序顺序的详细信息,请参阅Option Compare 语句For further information on binary comparisons, textual comparisons, and sort order, see Option Compare Statement.

Compare 参数的设置是:The Compare argument settings are:

返回的常量Constant 描述Description
Binary 根据从字符的内部二进制表示形式派生的排序顺序执行二进制比较。Performs a binary comparison, based on a sort order derived from the internal binary representations of the characters.
Text 根据系统的 LocaleID 值确定的不区分大小写的文本排序顺序执行文本比较。Performs a text comparison, based on a case-insensitive text sort order determined by your system's LocaleID value.