指定した 2 つの String オブジェクトの部分文字列を比較します。
Overloads Public Shared Function Compare( _
ByVal strA As String, _ ByVal indexA As Integer, _ ByVal strB As String, _ ByVal indexB As Integer, _ ByVal length As Integer _) As Integer
[C#]
public static int Compare(stringstrA,intindexA,stringstrB,intindexB,intlength);
[C++]
public: static int Compare(String* strA,intindexA,String* strB,intindexB,intlength);
[JScript]
public static function Compare(
strA : String,indexA : int,strB : String,indexB : int,length : int) : int;
パラメータ
- strA
第 1 の String 。 - indexA
strA 内の部分文字列の位置。 - strB
第 2 の String 。 - indexB
strB 内の部分文字列の位置。 - length
比較する各部分文字列の最大文字数。
戻り値
2 つの比較対照値の構文上の関係を示す 32 ビット符号付き整数。
値 | 意味 |
---|---|
0 より小さい | strA は strB よりも小さい部分文字列です。 |
0 | これらの部分文字列が等しいか、または length が 0 です。 |
0 より大 | strA 内の部分文字列が strB 内の部分文字列より大きいです。 |
例外
例外の種類 | 条件 |
---|---|
ArgumentOutOfRangeException | indexA が strA. Length より大きい値です。
または indexB が strB. Length より大きい値です。 または indexA 、 indexB 、または length が負の値です。 |
解説
比較する部分文字列は strA に indexA を足した位置、および strB に indexB を足した位置から開始されます。1 番目の部分文字列の長さは strA の長さから indexA を引いた値で、2 番目の部分文字列の長さは strB の長さから indexB を引いた値です。
比較する文字の数は、2 つの部分文字列の長さと length の中で、一番小さい値となります。 indexA 、 indexB 、および length の各パラメータが負数以外である必要があります。
比較では、現在のカルチャを使用して、大文字と小文字の規則や個々の文字のアルファベット順など、カルチャ固有の情報を取得します。たとえば、カルチャでは、1 つの文字として扱う特定の文字の組み合わせ、大文字と小文字を特定の方法で比較するかどうか、前後の文字に基づいた文字の並べ替えの基準などを規定します。
比較は、単語の並べ替え規則を使用して実行されます。単語、文字列、序数の並べ替えの詳細については、「 System.Globalization.CompareOptions 」を参照してください。
比較対象値の一方または両方を null 参照 (Visual Basic では Nothing) にできます。定義上、空文字列 ("") を含むすべての文字列は null 参照よりも大きく、また 2 つの null 参照は互いに等しくなります。
比較は、等しくない文字が検出されるか、両方の部分文字列すべてが比較された時点で終了します。ただし、2 つの文字列の比較で、一方の文字列の末尾までは等しく、もう一方の文字列に残りの文字がある場合、もう一方の文字列の方が大きいと見なされます。戻り値は、最後に実行された比較の結果です。
カルチャ固有の大文字/小文字の区別規則によって比較が影響される場合、予期しない結果が発生する可能性があります。たとえば、トルコ語の場合、トルコ語のファイル システムは "file" の文字 'i' に関して言語学的な大文字/小文字の区別規則を使用しないため、次の例では間違った結果が生成されます。
static String IsFileURI(String path) {
return (String.Compare(path, 0, "file:", 0, 5, true)== 0); }
パスの名前は変更できない方法で比較する必要があります。これを実行するための適切なコードは次のとおりです。
static String IsFileURI(String path) {
return (String.Compare(path, 0, "file:", 0, 5, true, CultureInfo.InvariantCulture)== 0); }
使用例
' Sample for String.Compare(String, Int32, String, Int32, Int32)
Imports System
Imports Microsoft.VisualBasic
Class Sample
Public Shared Sub Main()
' 0123456
Dim str1 As [String] = "machine"
Dim str2 As [String] = "device"
Dim str As [String]
Dim result As Integer
Console.WriteLine()
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2)
result = [String].Compare(str1, 2, str2, 0, 2)
str = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1)
Console.Write("{0} ", str)
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2)
End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'str1 = 'machine', str2 = 'device'
'Substring 'ch' in 'machine' is less than substring 'de' in 'device'.
'
[C#]
// Sample for String.Compare(String, Int32, String, Int32, Int32)
using System;
class Sample {
public static void Main() {
// 0123456
String str1 = "machine";
String str2 = "device";
String str;
int result;
Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
result = String.Compare(str1, 2, str2, 0, 2);
str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1);
Console.Write("{0} ", str);
Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(0, 2), str2);
}
}
/*
This example produces the following results:
str1 = 'machine', str2 = 'device'
Substring 'ch' in 'machine' is less than substring 'de' in 'device'.
*/
[C++]
// Sample for String::Compare(String, Int32, String, Int32, Int32)
#using <mscorlib.dll>
using namespace System;
int main() {
// 0123456
String* str1 = S"machine";
String* str2 = S"device";
String* str;
int result;
Console::WriteLine();
Console::WriteLine(S"str1 = '{0}', str2 = '{1}'", str1, str2);
result = String::Compare(str1, 2, str2, 0, 2);
str = ((result < 0) ? S"less than" : ((result > 0) ? S"greater than" : S"equal to"));
Console::Write(S"Substring '{0}' in ' {1}' is ", str1->Substring(2, 2), str1);
Console::Write(S" {0} ", str);
Console::WriteLine(S"substring '{0}' in ' {1}'.", str2->Substring(0, 2), str2);
}
/*
This example produces the following results:
str1 = 'machine', str2 = 'device'
Substring 'ch' in 'machine' is less than substring 'de' in 'device'.
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
参照
String クラス | String メンバ | System 名前空間 | String.Compare オーバーロードの一覧 | Int32 | CompareOrdinal | CompareTo