指定した 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, _ ByVal ignoreCase As Boolean _) As Integer
[C#]
public static int Compare(stringstrA,intindexA,stringstrB,intindexB,intlength,boolignoreCase);
[C++]
public: static int Compare(String* strA,intindexA,String* strB,intindexB,intlength,boolignoreCase);
[JScript]
public static function Compare(
strA : String,indexA : int,strB : String,indexB : int,length : int,ignoreCase : Boolean) : int;
パラメータ
- strA
第 1 の String 。 - indexA
strA 内の部分文字列の位置。 - strB
第 2 の String 。 - indexB
strB 内の部分文字列の位置。 - length
比較する各部分文字列の最大文字数。 - ignoreCase
大文字と小文字を区別して比較するか、区別せずに比較するかを示す Boolean 。(true は、大文字と小文字を区別せずに比較することを示します。
戻り値
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, Boolean)
Imports System
Imports Microsoft.VisualBasic
Class Sample
Public Shared Sub Main()
' 0123456
Dim str1 As [String] = "MACHINE"
Dim str2 As [String] = "machine"
Dim str As [String]
Dim result As Integer
Console.WriteLine()
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2)
Console.WriteLine("Ignore case:")
result = [String].Compare(str1, 2, str2, 2, 2, True)
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(2, 2), str2)
Console.WriteLine()
Console.WriteLine("Honor case:")
result = [String].Compare(str1, 2, str2, 2, 2, False)
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(2, 2), str2)
End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'str1 = 'MACHINE', str2 = 'machine'
'Ignore case:
'Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'.
'
'Honor case:
'Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'.
'
[C#]
// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean)
using System;
class Sample {
public static void Main() {
// 0123456
String str1 = "MACHINE";
String str2 = "machine";
String str;
int result;
Console.WriteLine();
Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
Console.WriteLine("Ignore case:");
result = String.Compare(str1, 2, str2, 2, 2, true);
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(2, 2), str2);
Console.WriteLine();
Console.WriteLine("Honor case:");
result = String.Compare(str1, 2, str2, 2, 2, false);
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(2, 2), str2);
}
}
/*
This example produces the following results:
str1 = 'MACHINE', str2 = 'machine'
Ignore case:
Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'.
Honor case:
Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'.
*/
[C++]
// Sample for String::Compare(String, Int32, String, Int32, Int32, Boolean)
#using <mscorlib.dll>
using namespace System;
int main() {
// 0123456
String* str1 = S"MACHINE";
String* str2 = S"machine";
String* str;
int result;
Console::WriteLine();
Console::WriteLine(S"str1 = '{0}', str2 = '{1}'", str1, str2);
Console::WriteLine(S"Ignore case:");
result = String::Compare(str1, 2, str2, 2, 2, true);
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(2, 2), str2);
Console::WriteLine();
Console::WriteLine(S"Honor case:");
result = String::Compare(str1, 2, str2, 2, 2, false);
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(2, 2), str2);
}
/*
This example produces the following results:
str1 = 'MACHINE', str2 = 'machine'
Ignore case:
Substring 'CH' in 'MACHINE' is equal to substring 'ch' in 'machine'.
Honor case:
Substring 'CH' in 'MACHINE' is greater than substring 'ch' in 'machine'.
*/
[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