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

定義

根據字串比較的結果傳回 -1、0 或 1。

C#
public static int StrComp (string? String1, string? String2, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);
C#
public static int StrComp (string String1, string String2, Microsoft.VisualBasic.CompareMethod Compare = Microsoft.VisualBasic.CompareMethod.Binary);

參數

String1
String

必要。 任何有效的 String 運算式。

String2
String

必要。 任何有效的 String 運算式。

Compare
CompareMethod

選擇性。 指定字串比較的類型。 如果省略了 Compare,則 Option Compare 設定可決定比較的類型。

傳回

下列其中一個值:

如果StrComp 會傳回
String1 排在 String2 之前-1
String1 等於 String20
String1 排在 String2 之後1

例外狀況

Compare 值無效。

範例

這個範例會使用函 StrComp 式傳回字串比較的結果。

VB
' 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)

備註

字串會以以第一個字元開頭的英數位元排序值進行比較。 如需二進位比較、文字比較和排序順序的詳細資訊,請參閱 Option Compare Statement

Compare 引數設定為︰

常數 描述
Binary 根據衍生自字元內部二進位表示法的排序順序,執行二進位比較。
Text 根據系統 LocaleID 值所決定的不區分大小寫的文字排序順序,執行文字比較。

適用於

產品 版本
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

另請參閱