根據字串比較的結果傳回 -1、0 或 1。
命名空間: Microsoft.VisualBasic
組件: Microsoft.VisualBasic (在 microsoft.visualbasic.dll 中)
語法
'宣告
Public Shared Function StrComp ( _
String1 As String, _
String2 As String, _
<OptionalAttribute> Optional Compare As CompareMethod = CompareMethod.Binary _
) As Integer
'用途
Dim String1 As String
Dim String2 As String
Dim Compare As CompareMethod
Dim returnValue As Integer
returnValue = Strings.StrComp(String1, String2, Compare)
public static int StrComp (
string String1,
string String2,
[OptionalAttribute] CompareMethod Compare
)
public:
static int StrComp (
String^ String1,
String^ String2,
[OptionalAttribute] CompareMethod Compare
)
public static int StrComp (
String String1,
String String2,
/** @attribute OptionalAttribute() */ CompareMethod Compare
)
public static function StrComp (
String1 : String,
String2 : String,
Compare : CompareMethod
) : int
參數
- String1
必要項。任何有效的 String 運算式。
- String2
必要項。任何有效的 String 運算式。
- Compare
選擇項。指定字串比較的類型。如果省略了 Compare,則 Option Compare 設定可決定比較的類型。
傳回值
如果 String1 排序在 String2, 之前,則 StrComp 會傳回 -1。如果 String1 等於 String2, ,則 StrComp 會傳回 0。如果 String1 排序在 String2, 之後,則 StrComp 會傳回 1。
備註
如需詳細資訊,請參閱 Visual Basic 的主題 StrComp 函式 (Visual Basic)。
會使用以第一個字元為開頭的英數排序值來比較字串。如需二進位比較、文字比較和排序次序的詳細資訊,請參閱 Option Compare 陳述式。
Compare 引數設定為:
常數 |
描述 |
|---|---|
Binary |
根據衍生自字元的內部二進位表示之排序次序,執行二進位比較。 |
Text |
根據系統的 LocaleID 值所決定的不區分大小寫的文字排序次序,執行文字比較。 |
範例
此範例使用 StrComp 函式,傳回字串比較的結果。如果省略了第三個引數,則會執行定義在 Option Compare 陳述式內的比較類型或專案預設值。
' 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 after TestStr2. Returns -1.
TestComp = StrComp(TestStr1, TestStr2, CompareMethod.Binary)
' TestStr2 sorts before TestStr1. Returns 1.
TestComp = StrComp(TestStr2, TestStr1)
平台
Windows 98、 Windows 2000 SP4、 Windows CE、 Windows Millennium Edition、 Windows Mobile for Pocket PC、 Windows Mobile for Smartphone、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition
.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。
版本資訊
.NET Framework
支援版本:2.0、1.1、1.0
.NET Compact Framework
支援版本:2.0、1.0
請參閱
參考
Strings 類別
Strings 成員
Microsoft.VisualBasic 命名空間
ArgumentException
其他資源
StrComp 函式 (Visual Basic)
字串操作摘要
Visual Basic 中的字串
Visual Basic 中的字串簡介
InStr 函式 (Visual Basic)