String.Compare メソッドとは? わかりやすく解説

String.Compare メソッド (String, Int32, String, Int32, Int32, Boolean)

指定した 2 つString オブジェクト部分文字列比較します。比較時に大文字小文字区別するかどうか設定できます

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function Compare
 ( _
    strA As String, _
    indexA As Integer, _
    strB As String, _
    indexB As Integer, _
    length As Integer, _
    ignoreCase As Boolean _
) As Integer
Dim strA As String
Dim indexA As Integer
Dim strB As String
Dim indexB As Integer
Dim length As Integer
Dim ignoreCase As Boolean
Dim returnValue As Integer

returnValue = String.Compare(strA, indexA, strB, indexB, length,
 ignoreCase)
public static int Compare
 (
    string strA,
    int indexA,
    string strB,
    int indexB,
    int length,
    bool ignoreCase
)
public:
static int Compare (
    String^ strA, 
    int indexA, 
    String^ strB, 
    int indexB, 
    int length, 
    bool ignoreCase
)
public static int Compare
 (
    String strA, 
    int indexA, 
    String strB, 
    int indexB, 
    int length, 
    boolean ignoreCase
)
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

indexAstrA より大きい。 Length.

または

indexBstrB より大きいLength.

または

indexAindexB、または length が負の値です。

解説解説

比較する部分文字列strAindexA足した位置、および strBindexB足した位置から開始されます。1 番目の部分文字列長さstrA長さから indexA引いた値で、2 番目の部分文字列長さstrB長さから indexB引いた値です。

比較する文字の数は、2 つ部分文字列長さlength の中で、一番小さい値となりますindexAindexB、および 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, StringComparison.OrdinalIgnoreCase)==
 0); }
使用例使用例

大文字と小文字表記だけが異な2 つ部分文字列比較する、2 とおりのコード例次に示します1 つ目の例では大文字と小文字違い無視して比較し2 つ目の例では大文字と小文字区別して比較してます。

' 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'.
'
// 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'.
*/
// Sample for String::Compare(String, Int32, String, Int32, Int32, Boolean)
using namespace System;
int 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) ? (String^)"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) ? (String^)"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'.
*/
// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean)
import System.*;

class Sample
{
    public static void main(String[]
 args)
    {
        //                 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);
    } //main
} //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'.
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Compare メソッド (String, Int32, String, Int32, Int32, Boolean, CultureInfo)

指定した 2 つString オブジェクト部分文字列比較します。大文字と小文字区別するかどうか指定と、比較影響を及ぼすカルチャ固有の情報使用します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function Compare
 ( _
    strA As String, _
    indexA As Integer, _
    strB As String, _
    indexB As Integer, _
    length As Integer, _
    ignoreCase As Boolean, _
    culture As CultureInfo _
) As Integer
Dim strA As String
Dim indexA As Integer
Dim strB As String
Dim indexB As Integer
Dim length As Integer
Dim ignoreCase As Boolean
Dim culture As CultureInfo
Dim returnValue As Integer

returnValue = String.Compare(strA, indexA, strB, indexB, length,
 ignoreCase, culture)
public static int Compare
 (
    string strA,
    int indexA,
    string strB,
    int indexB,
    int length,
    bool ignoreCase,
    CultureInfo culture
)
public:
static int Compare (
    String^ strA, 
    int indexA, 
    String^ strB, 
    int indexB, 
    int length, 
    bool ignoreCase, 
    CultureInfo^ culture
)
public static int Compare
 (
    String strA, 
    int indexA, 
    String strB, 
    int indexB, 
    int length, 
    boolean ignoreCase, 
    CultureInfo culture
)
public static function Compare
 (
    strA : String, 
    indexA : int, 
    strB : String, 
    indexB : int, 
    length : int, 
    ignoreCase : boolean, 
    culture : CultureInfo
) : int

パラメータ

strA

第 1 の String

indexA

strA 内の部分文字列位置

strB

第 2 の String

indexB

strB 内の部分文字列位置

length

比較する各部文字列最大文字数

ignoreCase

大文字と小文字区別して比較するか、区別せず比較するかを示す Boolean。(true は、大文字と小文字区別せず比較することを示します

culture

カルチャ固有の比較情報提供する CultureInfo オブジェクト

戻り値
2 つ比較対象値の関係を示す整数値。

条件

0 より小さい値

strA 内の部分文字列が、strB 内の部分文字列より小さいです

0

これらの部分文字列等しいか、または length が 0 です。

0 より大きい

strA 内の部分文字列strB 内の部分文字列より大きいです。

例外例外
例外種類条件

ArgumentOutOfRangeException

indexAstrA より大きい。 Length.

または

indexBstrB より大きいLength.

または

indexAindexB、または length が負の値です。

ArgumentNullException

culturenull 参照 (Visual Basic では Nothing) です。

解説解説

比較する部分文字列strAindexA足した位置、および strBindexB足した位置から開始されます。1 番目の部分文字列長さstrA長さから indexA引いた値で、2 番目の部分文字列長さstrB長さから indexB引いた値です。

比較する文字の数は、2 つ部分文字列長さlength の中で、一番小さい値となりますindexAindexB、および length の各パラメータ負数以外である必要があります

比較では、culture パラメータ使用して大文字と小文字規則個々文字アルファベット順など、カルチャ固有の情報取得します。たとえば、カルチャでは、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); }

序数に基づく比較使用して、"file" へのパス名を比較します。これを実行するための適切なコード次のとおりです。

 static String IsFileURI(String path) { 
    return (String.Compare(path, 0, "file:", 0, 5, true, StringComparison.OrdinalIgnoreCase)==
 0); }
使用例使用例

異なるカルチャを使用して2 つ部分文字列比較するコード例次に示します大文字と小文字違い無視してます。選択したカルチャによって、文字 'I' の比較方法違い生じます

' Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean,
 CultureInfo)
Imports System
Imports System.Globalization
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, Turkish culture:")
      result = [String].Compare(str1, 4, str2, 4, 2, True, New
 CultureInfo("tr-TR"))
      str = IIf(result < 0, "less than", IIf(result
 > 0, "greater than", "equal
 to"))
      Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4,
 2), str1)
      Console.Write("{0} ", str)
      Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4,
 2), str2)
      
      Console.WriteLine()
      Console.WriteLine("Ignore case, invariant culture:")
      result = [String].Compare(str1, 4, str2, 4, 2, True, CultureInfo.InvariantCulture)
      str = IIf(result < 0, "less than", IIf(result
 > 0, "greater than", "equal
 to"))
      Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4,
 2), str1)
      Console.Write("{0} ", str)
      Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4,
 2), str2)
   End Sub 'Main
End Class 'Sample
'
'This example produces the following results:
'
'str1 = 'MACHINE', str2 = 'machine'
'Ignore case, Turkish culture:
'Substring 'IN' in 'MACHINE' is less than substring 'in' in 'machine'.
'
'Ignore case, invariant culture:
'Substring 'IN' in 'MACHINE' is equal to substring 'in' in 'machine'.
'
// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean,
 CultureInfo)
using System;
using System.Globalization;

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, Turkish culture:");
    result = String.Compare(str1, 4, str2, 4, 2, true, new
 CultureInfo("tr-TR"));
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater
 than" : "equal to"));
    Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4,
 2), str1);
    Console.Write("{0} ", str);
    Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4,
 2), str2);

    Console.WriteLine();
    Console.WriteLine("Ignore case, invariant culture:");
    result = String.Compare(str1, 4, str2, 4, 2, true, CultureInfo.InvariantCulture);
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater
 than" : "equal to"));
    Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(4,
 2), str1);
    Console.Write("{0} ", str);
    Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(4,
 2), str2);
    }
}
/*
This example produces the following results:

str1 = 'MACHINE', str2 = 'machine'
Ignore case, Turkish culture:
Substring 'IN' in 'MACHINE' is less than substring 'in'
 in 'machine'.

Ignore case, invariant culture:
Substring 'IN' in 'MACHINE' is equal to substring 'in'
 in 'machine'.
*/
// Sample for String::Compare(String, Int32, String, Int32, Int32, Boolean,
 CultureInfo)
using namespace System;
using namespace System::Globalization;
int 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, Turkish culture:"
 );
   result = String::Compare( str1, 4, str2, 4, 2, true, gcnew
 CultureInfo( "tr-TR" ) );
   str = ((result < 0) ? "less than" : ((result > 0) ? (String^)"greater
 than" : "equal to"));
   Console::Write( "Substring '{0}' in '{1}' is ", str1->Substring(
 4, 2 ), str1 );
   Console::Write( " {0} ", str );
   Console::WriteLine( "substring '{0}' in '{1}'.",
 str2->Substring( 4, 2 ), str2 );
   Console::WriteLine();
   Console::WriteLine( "Ignore case, invariant culture:"
 );
   result = String::Compare( str1, 4, str2, 4, 2, true, CultureInfo::InvariantCulture
 );
   str = ((result < 0) ? "less than" : ((result > 0) ? (String^)"greater
 than" : "equal to"));
   Console::Write( "Substring '{0}' in '{1}' is ", str1->Substring(
 4, 2 ), str1 );
   Console::Write( " {0} ", str );
   Console::WriteLine( "substring '{0}' in '{1}'.",
 str2->Substring( 4, 2 ), str2 );
}

/*
This example produces the following results:

str1 = 'MACHINE', str2 = 'machine'
Ignore case, Turkish culture:
Substring 'IN' in 'MACHINE' is less than substring 'in'
 in 'machine'.

Ignore case, invariant culture:
Substring 'IN' in 'MACHINE' is equal to substring 'in'
 in 'machine'.
*/
// Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean,
 
// CultureInfo)
import System.*;
import System.Globalization.*;

class Sample
{
    public static void main(String[]
 args)
    {
        //                 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, Turkish culture:");
        result = String.Compare(str1, 4, str2, 4, 2, true, 
            new CultureInfo("tr-TR"));
        str = result < 0 ? "less than" : (result > 0) ? "greater
 than" : 
            "equal to";
        Console.Write("Substring '{0}' in '{1}' is ",
 str1.Substring(4, 2), 
            str1);
        Console.Write("{0} ", str);
        Console.WriteLine("substring '{0}' in '{1}'.",
 str2.Substring(4, 2), 
            str2);
        Console.WriteLine();

        Console.WriteLine("Ignore case, invariant culture:");
        result = String.Compare(str1, 4, str2, 4, 2, true, 
            CultureInfo.get_InvariantCulture());
        str = result < 0 ? "less than" : (result > 0) ? "greater
 than" : 
            "equal to";
        Console.Write("Substring '{0}' in '{1}' is ",
 str1.Substring(4, 2), 
            str1);
        Console.Write("{0} ", str);
        Console.WriteLine("substring '{0}' in '{1}'.",
 str2.Substring(4, 2), 
            str2);
    } //main
} //Sample
/*
This example produces the following results:

str1 = 'MACHINE', str2 = 'machine'
Ignore case, Turkish culture:
Substring 'IN' in 'MACHINE' is less than substring 'in'
 in 'machine'.

Ignore case, invariant culture:
Substring 'IN' in 'MACHINE' is equal to substring 'in'
 in 'machine'.
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Compare メソッド (String, Int32, String, Int32, Int32, StringComparison)

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定した 2 つString オブジェクト部分文字列比較します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function Compare
 ( _
    strA As String, _
    indexA As Integer, _
    strB As String, _
    indexB As Integer, _
    length As Integer, _
    comparisonType As StringComparison _
) As Integer
Dim strA As String
Dim indexA As Integer
Dim strB As String
Dim indexB As Integer
Dim length As Integer
Dim comparisonType As StringComparison
Dim returnValue As Integer

returnValue = String.Compare(strA, indexA, strB, indexB, length,
 comparisonType)
public static int Compare
 (
    string strA,
    int indexA,
    string strB,
    int indexB,
    int length,
    StringComparison comparisonType
)
public:
static int Compare (
    String^ strA, 
    int indexA, 
    String^ strB, 
    int indexB, 
    int length, 
    StringComparison comparisonType
)
public static int Compare
 (
    String strA, 
    int indexA, 
    String strB, 
    int indexB, 
    int length, 
    StringComparison comparisonType
)
public static function Compare
 (
    strA : String, 
    indexA : int, 
    strB : String, 
    indexB : int, 
    length : int, 
    comparisonType : StringComparison
) : int

パラメータ

strA

最初String オブジェクト

indexA

strA 内の部分文字列位置

strB

2 番目の String オブジェクト

indexB

strB 内の部分文字列位置

length

比較する各部文字列最大文字数

comparisonType

StringComparison 値の 1 つ

戻り値
2 つ比較対照値の構文上の関係を示す 32 ビット符号付き整数

例外例外
例外種類条件

ArgumentOutOfRangeException

indexAstrA より大きい。 Length.

または

indexBstrB より大きいLength.

または

indexAindexB、または length が負の値です。

ArgumentException

comparisonTypeStringComparison 値ではありません。

解説解説

比較する部分文字列strAindexA足した位置、および strBindexB足した位置から開始されます。1 番目の部分文字列長さstrA長さから indexA引いた値で、2 番目の部分文字列長さstrB長さから indexB引いた値です。

比較する文字の数は、2 つ部分文字列長さlength の中で、一番小さい値となりますindexAindexB、および length の各パラメータ負数以外である必要があります

comparisonType パラメータは、比較種類 (現在のカルチャを使用するかインバリアント カルチャを使用するか、比較対象値の大文字と小文字区別するか、並べ替え規則単語 (カルチャに依存) を使用する序数 (カルチャ非依存) を使用するか) を指定します

比較対象値の一方または両方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); }

序数に基づく比較使用して、"file" へのパス名を比較します。これを実行するための適切なコード次のとおりです。

 static String IsFileURI(String path) { 
    return (String.Compare(path, 0, "file:", 0, 5, true, StringComparison.OrdinalIgnoreCase)==
 0); }
使用例使用例

2 つ部分文字列比較するコード例次に示します

' 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'.
'
// 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'.
*/
// Sample for String::Compare(String, Int32, String, Int32, Int32)
using namespace System;
int 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) ? (String^)"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'.
*/
// Sample for String.Compare(String, Int32, String, Int32, Int32)
import System.*;

class Sample
{
    public static void main(String[]
 args)
    {
        //                 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);
    } //main
} //Sample
/*
This example produces the following results:

str1 = 'machine', str2 = 'device'
Substring 'ch' in 'machine' is less than substring 'de' in
 'device'.
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Compare メソッド (String, String)

指定した 2 つString オブジェクト比較します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

解説解説

比較では、現在のカルチャを使用して大文字と小文字規則個々文字アルファベット順など、カルチャ固有の情報取得します。たとえば、カルチャでは、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); }

序数に基づく比較使用して、"file" へのパス名を比較します。これを実行するための適切なコード次のとおりです。

 static String IsFileURI(String path) { 
    return (String.Compare(path, 0, "file:", 0, 5, true, StringComparison.OrdinalIgnoreCase)==
 0); }
使用例使用例

Compare メソッド使用して2 つ文字列評価する方法については、次の ReverseStringComparer クラスコード例参照してください

Imports System
Imports System.Text
Imports System.Collections



Public Class SamplesArrayList
    
    
    Public Shared Sub Main()
        Dim myAL As New
 ArrayList()
        ' Creates and initializes a new ArrayList.
        myAL.Add("Eric")
        myAL.Add("Mark")
        myAL.Add("Lance")
        myAL.Add("Rob")
        myAL.Add("Kris")
        myAL.Add("Brad")
        myAL.Add("Kit")
        myAL.Add("Bradley")
        myAL.Add("Keith")
        myAL.Add("Susan")
        
        ' Displays the properties and values of    the    ArrayList.
        Console.WriteLine("Count: {0}", myAL.Count)
        PrintValues("Unsorted", myAL)
        myAL.Sort()
        PrintValues("Sorted", myAL)
        Dim comp as New
 ReverseStringComparer
        myAL.Sort(comp)
        PrintValues("Reverse", myAL)

        Dim names As String()
 = CType(myAL.ToArray(GetType(String)), String())
    End Sub 'Main
   
   
    
    Public Shared Sub PrintValues(title
 As String, myList As IEnumerable)
        Console.Write("{0,10}: ", title)
        Dim sb As New StringBuilder()
        Dim s As String
        For Each s In  myList
            sb.AppendFormat("{0}, ", s)
        Next s
        sb.Remove(sb.Length - 2, 2)
        Console.WriteLine(sb)
    End Sub 'PrintValues
End Class 'SamplesArrayList

Public Class ReverseStringComparer 
  Implements IComparer
    
     Function Compare(x As Object,
 y As Object) As Integer
 implements IComparer.Compare
        Dim s1 As String
 = CStr (x)
        Dim s2 As String
 = CStr (y)
        
        'negate the return value to get the reverse order
        Return - [String].Compare(s1, s2)
    
    End Function 'Compare
End Class 'ReverseStringComparer

using System;
using System.Text;
using System.Collections;

public class SamplesArrayList  {

    public static void Main()
  {
        // Creates and initializes a new ArrayList.
        ArrayList myAL = new ArrayList();
        myAL.Add("Eric");
        myAL.Add("Mark");
        myAL.Add("Lance");
        myAL.Add("Rob");
        myAL.Add("Kris");
        myAL.Add("Brad");
        myAL.Add("Kit");
        myAL.Add("Bradley");
        myAL.Add("Keith");
        myAL.Add("Susan");
    
        // Displays the properties and values of    the    ArrayList.
        Console.WriteLine( "Count: {0}", myAL.Count );
        
        PrintValues ("Unsorted", myAL );
        myAL.Sort();
        PrintValues("Sorted", myAL );
        myAL.Sort(new ReverseStringComparer() );
        PrintValues ("Reverse" , myAL );


        string [] names = (string[]) myAL.ToArray
 (typeof(string));


    }
    public static void PrintValues(string
 title, IEnumerable    myList )  {
        Console.Write ("{0,10}: ", title);
        StringBuilder sb = new StringBuilder();
        foreach (string s in
 myList) {
            sb.AppendFormat( "{0}, ", s);
        }
        sb.Remove (sb.Length-2,2);
        Console.WriteLine(sb);
    }
}
public class ReverseStringComparer : IComparer
 {
   public int Compare (object x, object y)
 {
       string s1 = x as string;
       string s2 = y as string;      
       //negate the return value to get the reverse order
       return - String.Compare (s1,s2);

   }
}

using namespace System;
using namespace System::Text;
using namespace System::Collections;

ref class ReverseStringComparer: public IComparer
{
public:
   virtual int Compare( Object^ x, Object^ y )
   {
      String^ s1 = dynamic_cast<String^>(x);
      String^ s2 = dynamic_cast<String^>(y);

      //negate the return value to get the reverse order
      return  -String::Compare( s1, s2 );
   }

};

void PrintValues( String^ title, IEnumerable^ myList )
{
   Console::Write( "{0,10}: ", title );
   StringBuilder^ sb = gcnew StringBuilder;
   {
      IEnumerator^ en = myList->GetEnumerator();
      String^ s;
      while ( en->MoveNext() )
      {
         s = en->Current->ToString();
         sb->AppendFormat(  "{0}, ", s );
      }
   }
   sb->Remove( sb->Length - 2, 2 );
   Console::WriteLine( sb );
}

void main()
{
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew ArrayList;
   myAL->Add( "Eric" );
   myAL->Add( "Mark" );
   myAL->Add( "Lance" );
   myAL->Add( "Rob" );
   myAL->Add( "Kris" );
   myAL->Add( "Brad" );
   myAL->Add( "Kit" );
   myAL->Add( "Bradley" );
   myAL->Add( "Keith" );
   myAL->Add( "Susan" );

   // Displays the properties and values of the ArrayList.
   Console::WriteLine( "Count: {0}", myAL->Count.ToString() );

   PrintValues( "Unsorted", myAL );

   myAL->Sort();
   PrintValues( "Sorted", myAL );

   myAL->Sort( gcnew ReverseStringComparer );
   PrintValues( "Reverse", myAL );

   array<String^>^names = dynamic_cast<array<String^>^>(myAL->ToArray(
 String::typeid ));
}
import System.*;
import System.Text.*;
import System.Collections.*;

public class SamplesArrayList
{
    public static void main(String[]
 args)
    {
        // Creates and initializes a new ArrayList.
        ArrayList myAL = new ArrayList();

        myAL.Add("Eric");
        myAL.Add("Mark");
        myAL.Add("Lance");
        myAL.Add("Rob");
        myAL.Add("Kris");
        myAL.Add("Brad");
        myAL.Add("Kit");
        myAL.Add("Bradley");
        myAL.Add("Keith");
        myAL.Add("Susan");

        // Displays the properties and values of the ArrayList.
        Console.WriteLine("Count: {0}", (Int32)myAL.get_Count());

        PrintValues("Unsorted", myAL);

        myAL.Sort();
        PrintValues("Sorted", myAL);

        myAL.Sort(new ReverseStringComparer());
        PrintValues("Reverse", myAL);

        String names[] = (String[])(myAL.ToArray(String.class.ToType()));
    } //main
    
    public static void PrintValues(String
 title, IEnumerable myList)
    {
        Console.Write("{0,10}: ", title);
        StringBuilder sb = new StringBuilder();
        IEnumerator objEnum = myList.GetEnumerator();
        while (objEnum.MoveNext()) {
            String s = System.Convert.ToString(objEnum.get_Current());
            sb.AppendFormat("{0}, ", s);
        }

        sb.Remove(sb.get_Length() - 2, 2);
        Console.WriteLine(sb);
    } //PrintValues
} //SamplesArrayList
 

public class ReverseStringComparer implements
 IComparer
{
    public int Compare(Object x, Object y)
    {
        String s1 = System.Convert.ToString(x);
        String s2 = System.Convert.ToString(y);

        //negate the return value to get the reverse order
        return -String.Compare(s1, s2);
    } //Compare 
} //ReverseStringComparer
import System;
import System.Text;
import System.Collections;

public class SamplesArrayList  {

    public static function
 Main() : void {
        // Creates and initializes a new ArrayList.
        var myAL : ArrayList = new ArrayList();
        myAL.Add("Eric");
        myAL.Add("Mark");
        myAL.Add("Lance");
        myAL.Add("Rob");
        myAL.Add("Kris");
        myAL.Add("Brad");
        myAL.Add("Kit");
        myAL.Add("Bradley");
        myAL.Add("Keith");
        myAL.Add("Susan");
    
        // Displays the properties and values of    the    ArrayList.
        Console.WriteLine( "Count: {0}", myAL.Count );
        
        PrintValues ("Unsorted", myAL );
        myAL.Sort();
        PrintValues("Sorted", myAL );
        myAL.Sort(new ReverseStringComparer() );
        PrintValues ("Reverse" , myAL );


        var names : String [] = (String[])(myAL.ToArray (System.String));


    }
    public static function
 PrintValues(title : String, myList: IEnumerable ) : void {
        Console.Write ("{0,10}: ", title);
        var sb : StringBuilder = new StringBuilder();
        for (var s : String in myList) {
            sb.AppendFormat( "{0}, ", s);
        }
        sb.Remove (sb.Length-2,2);
        Console.WriteLine(sb);
    }
}
public class ReverseStringComparer implements
 IComparer {
   public function Compare (x, y) : int
  {
       //negate the return value to get the reverse order
       return - String.Compare (String(x), String(y));
   }
}
SamplesArrayList.Main();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Compare メソッド (String, String, StringComparison)

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定した 2 つString オブジェクト比較します。比較現在のカルチャを使用するかインバリアント カルチャを使用するか、大文字と小文字区別するか、並べ替え規則単語ベースにするか序数ベースにするかは、パラメータ指定します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function Compare
 ( _
    strA As String, _
    strB As String, _
    comparisonType As StringComparison _
) As Integer
Dim strA As String
Dim strB As String
Dim comparisonType As StringComparison
Dim returnValue As Integer

returnValue = String.Compare(strA, strB, comparisonType)
public static int Compare
 (
    string strA,
    string strB,
    StringComparison comparisonType
)
public:
static int Compare (
    String^ strA, 
    String^ strB, 
    StringComparison comparisonType
)
public static int Compare
 (
    String strA, 
    String strB, 
    StringComparison comparisonType
)
public static function Compare
 (
    strA : String, 
    strB : String, 
    comparisonType : StringComparison
) : int

パラメータ

strA

最初String オブジェクト

strB

2 番目の String オブジェクト

comparisonType

System.StringComparison 値の 1 つ

戻り値
2 つ比較対照値の構文上の関係を示す 32 ビット符号付き整数

条件

0 より小さい値

strAstrB より小さい。

0

strAstrB等しい。

0 より大きい

strAstrB より大きい

例外例外
例外種類条件

ArgumentException

comparisonTypeStringComparison 値ではありません。

解説解説

comparisonType パラメータは、比較種類 (現在のカルチャを使用するかインバリアント カルチャを使用するか、比較対象値の大文字と小文字区別するか、並べ替え規則単語 (カルチャに依存) を使用する序数 (カルチャ非依存) を使用するか) を指定します

比較対象値の一方または両方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); }

序数に基づく比較使用して、"file" へのパス名を比較します。これを実行するための適切なコード次のとおりです。

 static String IsFileURI(String path) { 
    return (String.Compare(path, 0, "file:", 0, 5, true, StringComparison.OrdinalIgnoreCase)==
 0); }
使用例使用例

3 種類の 'I' 文字比較するコード例次に示します選択したカルチャ、大文字と小文字区別比較種類 (序数ベースにしたかどうか) が実行結果影響します

' This example demonstrates the 
' System.String.Compare(String, String, StringComparison) method.

Imports System
Imports System.Threading

Class Sample
    Public Shared Sub Main()
 
        Dim intro As String
 = "Compare three versions of the letter I using different "
 & _
                              "values of StringComparison."
        
        ' Define an array of strings where each element contains a version
 of the 
        ' letter I. (An array of strings is used so you can easily modify
 this 
        ' code example to test additional or different combinations
 of strings.)  
        Dim threeIs(2) As String
        ' LATIN SMALL LETTER I (U+0069)
        threeIs(0) = "i"
        ' LATIN SMALL LETTER DOTLESS I (U+0131)
        threeIs(1) = "ı"
        ' LATIN CAPITAL LETTER I (U+0049)
        threeIs(2) = "I"
        
        Dim unicodeNames As String()
 =  { _
                            "LATIN SMALL LETTER I (U+0069)",
 _
                            "LATIN SMALL LETTER DOTLESS I (U+0131)",
 _
                            "LATIN CAPITAL LETTER I (U+0049)"
 }
        
        Dim scValues As StringComparison()
 =  { _
                            StringComparison.CurrentCulture, _
                            StringComparison.CurrentCultureIgnoreCase, _
                            StringComparison.InvariantCulture, _
                            StringComparison.InvariantCultureIgnoreCase, _
                            StringComparison.Ordinal, _
                            StringComparison.OrdinalIgnoreCase }
        '
        Console.Clear()
        Console.WriteLine(intro)
        
        ' Display the current culture because the culture-specific comparisons
        ' can produce different results with different cultures.
        Console.WriteLine("The current culture is {0}."
 & vbCrLf, _
                           Thread.CurrentThread.CurrentCulture.Name)
        
        ' Determine the relative sort order of three versions of the
 letter I. 
        Dim sc As StringComparison
        For Each sc In 
 scValues
            Console.WriteLine("StringComparison.{0}:",
 sc)
            
            ' LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS
 I (U+0131)
            Test(0, 1, sc, threeIs, unicodeNames)
            
            ' LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I
 (U+0049)
            Test(0, 2, sc, threeIs, unicodeNames)
            
            ' LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL
 LETTER I (U+0049)
            Test(1, 2, sc, threeIs, unicodeNames)
            
            Console.WriteLine()
        Next sc
    
    End Sub 'Main
    
    Protected Shared Sub
 Test(ByVal x As Integer,
 ByVal y As Integer, _
                              ByVal comparison As
 StringComparison, _
                              ByVal testI() As
 String, ByVal testNames() As
 String) 
        Dim resultFmt As String
 = "{0} is {1} {2}"
        Dim result As String
 = "equal to"
        Dim cmpValue As Integer
 = 0
        '
        cmpValue = String.Compare(testI(x), testI(y), comparison)
        If cmpValue < 0 Then
            result = "less than"
        ElseIf cmpValue > 0 Then
            result = "greater than"
        End If
        Console.WriteLine(resultFmt, testNames(x), result, testNames(y))
    
    End Sub 'Test
End Class 'Sample

'
'This code example produces the following results:
'
'Compare three versions of the letter I using different values of StringComparison.
'The current culture is en-US.
'
'StringComparison.CurrentCulture:
'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS
 I (U+0131)
'LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL
 LETTER I (U+0049)
'
'StringComparison.CurrentCultureIgnoreCase:
'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS
 I (U+0131)
'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL
 LETTER I (U+0049)
'
'StringComparison.InvariantCulture:
'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS
 I (U+0131)
'LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL
 LETTER I (U+0049)
'
'StringComparison.InvariantCultureIgnoreCase:
'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS
 I (U+0131)
'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL
 LETTER I (U+0049)
'
'StringComparison.Ordinal:
'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS
 I (U+0131)
'LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER
 I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL
 LETTER I (U+0049)
'
'StringComparison.OrdinalIgnoreCase:
'LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS
 I (U+0131)
'LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
'LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL
 LETTER I (U+0049)
'
// This example demonstrates the 
// System.String.Compare(String, String, StringComparison) method.

using System;
using System.Threading;

class Sample 
{
    public static void Main()
 
    {
    string intro = "Compare three versions of the letter
 I using different " + 
                   "values of StringComparison.";

// Define an array of strings where each element contains a version
 of the 
// letter I. (An array of strings is used so you can easily modify this
 
// code example to test additional or different combinations of strings.)
  

    string[] threeIs = new string[3];
// LATIN SMALL LETTER I (U+0069)
    threeIs[0] = "\u0069";
// LATIN SMALL LETTER DOTLESS I (U+0131)
    threeIs[1] = "\u0131";
// LATIN CAPITAL LETTER I (U+0049)
    threeIs[2] = "\u0049";

    string[] unicodeNames = 
             {
             "LATIN SMALL LETTER I (U+0069)", 
             "LATIN SMALL LETTER DOTLESS I (U+0131)", 
             "LATIN CAPITAL LETTER I (U+0049)"
             };

    StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };

//
    Console.Clear();
    Console.WriteLine(intro);

// Display the current culture because the culture-specific comparisons
// can produce different results with different cultures.
    Console.WriteLine("The current culture is {0}.\n", 
                       Thread.CurrentThread.CurrentCulture.Name);

// Determine the relative sort order of three versions of the letter
 I. 
    foreach (StringComparison sc in scValues)
        {
        Console.WriteLine("StringComparison.{0}:", sc);

// LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS I (U+0131)
        Test(0, 1, sc, threeIs, unicodeNames);

// LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049)
        Test(0, 2, sc, threeIs, unicodeNames);

// LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER I (U+0049)
        Test(1, 2, sc, threeIs, unicodeNames);

        Console.WriteLine();
        }
    }

    protected static void
 Test(int x, int y, 
                               StringComparison comparison, 
                               string[] testI, string[]
 testNames)
    {
    string resultFmt = "{0} is {1} {2}";
    string result = "equal to";
    int cmpValue = 0;
//
    cmpValue = String.Compare(testI[x], testI[y], comparison);
    if      (cmpValue < 0) 
        result = "less than";
    else if (cmpValue > 0)
        result = "greater than";
    Console.WriteLine(resultFmt, testNames[x], result, testNames[y]);
    }
}

/*
This code example produces the following results:

Compare three versions of the letter I using different values
 of StringComparison.
The current culture is en-US.

StringComparison.CurrentCulture:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.CurrentCultureIgnoreCase:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.InvariantCulture:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.InvariantCultureIgnoreCase:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.Ordinal:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

StringComparison.OrdinalIgnoreCase:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN CAPITAL LETTER I (U+0049)

*/
// This example demonstrates the
// System.String.Compare(String, String, StringComparison) method.

using namespace System;
using namespace System::Threading;

void Test(int testStringIndex, int
 searchStringIndex, 
          StringComparison comparison, array<String^>^ testI, 
          array<String^>^ testNames)
{
    String^ resultFormat = "{0} is {1} {2}";
    String^ resultString = "equal to";
    int comparisonValue = 0;

    comparisonValue = String::Compare(testI[testStringIndex],
        testI[searchStringIndex], comparison);
    if (comparisonValue < 0)
    {
        resultString = "less than";
    }
    else if (comparisonValue > 0)
    {
        resultString = "greater than";
    }
    Console::WriteLine(resultFormat, testNames[testStringIndex], resultString,
        testNames[searchStringIndex]);
}

int main()
{
    String^ introMessage =
        "Compare three versions of the letter I using different
 " +
        "values of StringComparison.";

    // Define an array of strings where each element contains a version
 of
    // the letter I. (An array of strings is used so you can easily
 modify
    // this code example to test additional or different combinations
 of
    // strings.)

    array<String^>^ letterVariation = gcnew array<String^>(3);
    // LATIN SMALL LETTER I (U+0069)
    letterVariation[0] = "i";
    // LATIN SMALL LETTER DOTLESS I (U+0131)
    letterVariation[1] = L"\u0131";
    // LATIN CAPITAL LETTER I (U+0049)
    letterVariation[2] = "I";

    array<String^>^ unicodeNames = {
        "LATIN SMALL LETTER I (U+0069)",
        "LATIN SMALL LETTER DOTLESS I (U+0131)",
        "LATIN CAPITAL LETTER I (U+0049)"};

    array<StringComparison>^ comparisonValues = {
        StringComparison::CurrentCulture,
        StringComparison::CurrentCultureIgnoreCase,
        StringComparison::InvariantCulture,
        StringComparison::InvariantCultureIgnoreCase,
        StringComparison::Ordinal,
        StringComparison::OrdinalIgnoreCase};

    Console::Clear();
    Console::WriteLine(introMessage);

    // Display the current culture because the culture-specific comparisons
    // can produce different results with different cultures.
    Console::WriteLine("The current culture is {0}.{1}",
        Thread::CurrentThread->CurrentCulture->Name, Environment::NewLine);

    // Determine the relative sort order of three versions of the letter
 I.
    for each (StringComparison stringCmp in
 comparisonValues)
    {
        Console::WriteLine("StringComparison.{0}:", stringCmp);

        // LATIN SMALL LETTER I (U+0069) : LATIN SMALL LETTER DOTLESS
 I
        // (U+0131)
        Test(0, 1, stringCmp, letterVariation, unicodeNames);

        // LATIN SMALL LETTER I (U+0069) : LATIN CAPITAL LETTER I (U+0049)
        Test(0, 2, stringCmp, letterVariation, unicodeNames);

        // LATIN SMALL LETTER DOTLESS I (U+0131) : LATIN CAPITAL LETTER
 I
        // (U+0049)
        Test(1, 2, stringCmp, letterVariation, unicodeNames);

        Console::WriteLine();
    }
}

/*
This code example produces the following results:

Compare three versions of the letter I using different values
 of 
StringComparison.
The current culture is en-US.

StringComparison.CurrentCulture:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER 
  DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN 
  CAPITAL LETTER I (U+0049)

StringComparison.CurrentCultureIgnoreCase:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER 
  DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN 
  CAPITAL LETTER I (U+0049)

StringComparison.InvariantCulture:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER 
  DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is less than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN 
  CAPITAL LETTER I (U+0049)

StringComparison.InvariantCultureIgnoreCase:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER 
  DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN 
  CAPITAL LETTER I (U+0049)

StringComparison.Ordinal:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER 
  DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is greater than LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN 
  CAPITAL LETTER I (U+0049)

StringComparison.OrdinalIgnoreCase:
LATIN SMALL LETTER I (U+0069) is less than LATIN SMALL LETTER 
  DOTLESS I (U+0131)
LATIN SMALL LETTER I (U+0069) is equal to LATIN CAPITAL LETTER I (U+0049)
LATIN SMALL LETTER DOTLESS I (U+0131) is greater than LATIN 
  CAPITAL LETTER I (U+0049)

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Compare メソッド (String, String, Boolean, CultureInfo)

指定した 2 つString オブジェクト比較します。大文字と小文字区別するかどうか指定と、比較影響を及ぼすカルチャ固有の情報使用します

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function Compare
 ( _
    strA As String, _
    strB As String, _
    ignoreCase As Boolean, _
    culture As CultureInfo _
) As Integer
Dim strA As String
Dim strB As String
Dim ignoreCase As Boolean
Dim culture As CultureInfo
Dim returnValue As Integer

returnValue = String.Compare(strA, strB, ignoreCase, culture)
public static int Compare
 (
    string strA,
    string strB,
    bool ignoreCase,
    CultureInfo culture
)
public:
static int Compare (
    String^ strA, 
    String^ strB, 
    bool ignoreCase, 
    CultureInfo^ culture
)
public static int Compare
 (
    String strA, 
    String strB, 
    boolean ignoreCase, 
    CultureInfo culture
)
public static function Compare
 (
    strA : String, 
    strB : String, 
    ignoreCase : boolean, 
    culture : CultureInfo
) : int

パラメータ

strA

第 1 の String

strB

第 2 の String

ignoreCase

大文字と小文字区別して比較するか、区別せず比較するかを示す Boolean。(true は、大文字と小文字区別せず比較することを示します

culture

カルチャ固有の比較情報提供する CultureInfo オブジェクト

戻り値
2 つ比較対照値の構文上の関係を示す 32 ビット符号付き整数

条件

0 より小さい値

strAstrB より小さい。

0

strAstrB等しい。

0 より大きい

strAstrB より大きい

例外例外
例外種類条件

ArgumentNullException

culturenull 参照 (Visual Basic では Nothing) です。

解説解説

比較では、culture パラメータ使用して大文字と小文字規則個々文字アルファベット順など、カルチャ固有の情報取得します。たとえば、カルチャでは、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); }

序数に基づく比較使用して、"file" へのパス名を比較します。これを実行するための適切なコード次のとおりです。

 static String IsFileURI(String path) { 
    return (String.Compare(path, 0, "file:", 0, 5, true, StringComparison.OrdinalIgnoreCase)==
 0); }
使用例使用例

カルチャが比較影響を及ぼすコード例次に示しますチェコ語 - チェコ共和国のカルチャでは、'ch' は 'd' よりも大き単一文字です。しかし、英語 - 米国のカルチャでは、"ch" は 2 つ文字から成り、'c' は 'd' より小さくなります

Imports System
Imports System.Globalization
 _

Class Sample
   Public Shared Sub Main()
      Dim str1 As [String] = "change"
      Dim str2 As [String] = "dollar"
      Dim relation As [String] = Nothing
      
      relation = symbol([String].Compare(str1, str2, False, New
 CultureInfo("en-US")))
      Console.WriteLine("For en-US: {0} {1} {2}",
 str1, relation, str2)
      
      relation = symbol([String].Compare(str1, str2, False, New
 CultureInfo("cs-CZ")))
      Console.WriteLine("For cs-CZ: {0} {1} {2}",
 str1, relation, str2)
   End Sub 'Main
   
   Private Shared Function
 symbol(r As Integer) As
 [String]
      Dim s As [String] = "="
      If r < 0 Then
         s = "<"
      Else
         If r > 0 Then
            s = ">"
         End If
      End If
      Return s
   End Function 'symbol
End Class 'Sample
'
'This example produces the following results.
'For en-US: change < dollar
'For cs-CZ: change > dollar
'
using System;
using System.Globalization;

class Sample {
    public static void Main()
 {
    String str1 = "change";
    String str2 = "dollar";
    String relation = null;

    relation = symbol( String.Compare(str1, str2, false, new
 CultureInfo("en-US")) );
    Console.WriteLine("For en-US: {0} {1} {2}", str1, relation, str2);

    relation = symbol( String.Compare(str1, str2, false, new
 CultureInfo("cs-CZ")) );
    Console.WriteLine("For cs-CZ: {0} {1} {2}", str1, relation, str2);
    }

    private static String symbol(int
 r) {
    String s = "=";
    if      (r < 0) s = "<";
    else if (r > 0) s = ">";
    return s;
    }
}
/*
This example produces the following results.
For en-US: change < dollar
For cs-CZ: change > dollar
*/
using namespace System;
using namespace System::Globalization;
String^ symbol( int r )
{
   String^ s = "=";
   if ( r < 0 )
      s = "<";
   else
   if ( r > 0 )
      s = ">";


   return s;
}

int main()
{
   String^ str1 = "change";
   String^ str2 = "dollar";
   String^ relation = nullptr;
   relation = symbol( String::Compare( str1, str2, false, gcnew
 CultureInfo( "en-US" ) ) );
   Console::WriteLine( "For en-US: {0} {1} {2}", str1, relation, str2 );
   relation = symbol( String::Compare( str1, str2, false, gcnew
 CultureInfo( "cs-CZ" ) ) );
   Console::WriteLine( "For cs-CZ: {0} {1} {2}", str1, relation, str2 );
}

/*
This example produces the following results.
For en-US: change < dollar
For cs-CZ: change > dollar
*/
import System.*;
import System.Globalization.*;

class Sample
{
    public static void main(String[]
 args)
    {
        String str1 = "change";
        String str2 = "dollar";
        String relation = null;

        relation = Symbol(String.Compare(str1, str2, false, 
            new CultureInfo("en-US")));
        Console.WriteLine("For en-US: {0} {1} {2}", str1, relation, str2);

        relation = Symbol(String.Compare(str1, str2, false, 
            new CultureInfo("cs-CZ")));
        Console.WriteLine("For cs-CZ: {0} {1} {2}", str1, relation, str2);
    } //main

    private static String Symbol(int
 r)
    {
        String s = "=";
        if (r < 0) {
            s = "<";
        }
        else {
            if (r > 0) {
                s = ">";
            }
        }
        return s;
    } //Symbol
} //Sample
/*
This example produces the following results.
For en-US: change < dollar
For cs-CZ: change > dollar
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Compare メソッド

指定した 2 つString オブジェクト比較します。
オーバーロードの一覧オーバーロードの一覧

名前 説明
String.Compare (String, String) 指定した 2 つString オブジェクト比較します。

.NET Compact Framework によってサポートされています。

String.Compare (String, String, Boolean) 指定した 2 つString オブジェクト比較します。比較時に大文字小文字区別するかどうか設定できます

.NET Compact Framework によってサポートされています。

String.Compare (String, String, StringComparison) 指定した 2 つString オブジェクト比較します。比較現在のカルチャを使用するかインバリアント カルチャを使用するか、大文字と小文字区別するか、並べ替え規則単語ベースにするか序数ベースにするかは、パラメータ指定します

.NET Compact Framework によってサポートされています。

String.Compare (String, String, Boolean, CultureInfo) 指定した 2 つString オブジェクト比較します。大文字と小文字区別するかどうか指定と、比較影響を及ぼすカルチャ固有の情報使用します

.NET Compact Framework によってサポートされています。

String.Compare (String, Int32, String, Int32, Int32) 指定した 2 つString オブジェクト部分文字列比較します。

.NET Compact Framework によってサポートされています。

String.Compare (String, Int32, String, Int32, Int32, Boolean) 指定した 2 つString オブジェクト部分文字列比較します。比較時に大文字小文字区別するかどうか設定できます

.NET Compact Framework によってサポートされています。

String.Compare (String, Int32, String, Int32, Int32, StringComparison) 指定した 2 つString オブジェクト部分文字列比較します。

.NET Compact Framework によってサポートされています。

String.Compare (String, Int32, String, Int32, Int32, Boolean, CultureInfo) 指定した 2 つString オブジェクト部分文字列比較します。大文字と小文字区別するかどうか指定と、比較影響を及ぼすカルチャ固有の情報使用します

.NET Compact Framework によってサポートされています。

参照参照

String.Compare メソッド (String, Int32, String, Int32, Int32)

指定した 2 つString オブジェクト部分文字列比較します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function Compare
 ( _
    strA As String, _
    indexA As Integer, _
    strB As String, _
    indexB As Integer, _
    length As Integer _
) As Integer
Dim strA As String
Dim indexA As Integer
Dim strB As String
Dim indexB As Integer
Dim length As Integer
Dim returnValue As Integer

returnValue = String.Compare(strA, indexA, strB, indexB, length)
public static int Compare
 (
    string strA,
    int indexA,
    string strB,
    int indexB,
    int length
)
public:
static int Compare (
    String^ strA, 
    int indexA, 
    String^ strB, 
    int indexB, 
    int length
)
public static int Compare
 (
    String strA, 
    int indexA, 
    String strB, 
    int indexB, 
    int length
)
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

indexAstrA より大きい。 Length.

または

indexBstrB より大きいLength.

または

indexAindexB、または length が負の値です。

解説解説

比較する部分文字列strAindexA足した位置、および strBindexB足した位置から開始されます。1 番目の部分文字列長さstrA長さから indexA引いた値で、2 番目の部分文字列長さstrB長さから indexB引いた値です。

比較する文字の数は、2 つ部分文字列長さlength の中で、一番小さい値となりますindexAindexB、および 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); }

序数に基づく比較使用して、"file" へのパス名を比較します。これを実行するための適切なコード次のとおりです。

 static String IsFileURI(String path) { 
    return (String.Compare(path, 0, "file:", 0, 5, true, StringComparison.OrdinalIgnoreCase)==
 0); }
使用例使用例

2 つ部分文字列比較するコード例次に示します

' 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'.
'
// 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'.
*/
// Sample for String::Compare(String, Int32, String, Int32, Int32)
using namespace System;
int 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) ? (String^)"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'.
*/
// Sample for String.Compare(String, Int32, String, Int32, Int32)
import System.*;

class Sample
{
    public static void main(String[]
 args)
    {
        //                 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);
    } //main
} //Sample
/*
This example produces the following results:

str1 = 'machine', str2 = 'device'
Substring 'ch' in 'machine' is less than substring 'de' in
 'device'.
*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Compare メソッド (String, String, Boolean)

指定した 2 つString オブジェクト比較します。比較時に大文字小文字区別するかどうか設定できます

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function Compare
 ( _
    strA As String, _
    strB As String, _
    ignoreCase As Boolean _
) As Integer
Dim strA As String
Dim strB As String
Dim ignoreCase As Boolean
Dim returnValue As Integer

returnValue = String.Compare(strA, strB, ignoreCase)
public static int Compare
 (
    string strA,
    string strB,
    bool ignoreCase
)
public:
static int Compare (
    String^ strA, 
    String^ strB, 
    bool ignoreCase
)

パラメータ

strA

第 1 の String

strB

第 2 の String

ignoreCase

大文字と小文字区別して比較するか、区別せず比較するかを示す Boolean。(true は、大文字と小文字区別せず比較することを示します

戻り値
2 つ比較対照値の構文上の関係を示す 32 ビット符号付き整数

条件

0 より小さい値

strAstrB より小さい。

0

strAstrB等しい。

0 より大きい

strAstrB より大きい

解説解説

比較では、現在のカルチャを使用して大文字と小文字規則個々文字アルファベット順など、カルチャ固有の情報取得します。たとえば、カルチャでは、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); }

序数に基づく比較使用して、"file" へのパス名を比較します。これを実行するための適切なコード次のとおりです。

 static String IsFileURI(String path) { 
    return (String.Compare(path, 0, "file:", 0, 5, true, StringComparison.OrdinalIgnoreCase)==
 0); }
使用例使用例

文字列比較する場合Compare メソッド使用しても、ToUpper または ToLower を使用しても同じであることを次のコード例示します

unsafe
{
    // Null terminated ASCII characters in an sbyte array
    String szAsciiUpper = null;
    sbyte[] sbArr1 = new sbyte[] { 0x41, 0x42, 0x43, 0x00 };
    // Instruct the Garbage Collector not to move the memory
    fixed(sbyte* pAsciiUpper = sbArr1)
    {
        szAsciiUpper = new String(pAsciiUpper);
    }
    String szAsciiLower = null;
    sbyte[] sbArr2 = { 0x61, 0x62, 0x63, 0x00 };
    // Instruct the Garbage Collector not to move the memory
    fixed(sbyte* pAsciiLower = sbArr2)
    {
        szAsciiLower = new String(pAsciiLower, 0, sbArr2.Length);
    }
    // Prints "ABC abc"
    Console.WriteLine(szAsciiUpper + " " + szAsciiLower);

    // Compare Strings - the result is true
    Console.WriteLine("The Strings are equal when capitalized ? " +
        (String.Compare(szAsciiUpper.ToUpper(), szAsciiLower.ToUpper())==0?"true":"false")
 );

    // This is the effective equivalent of another Compare method, which
 ignores case
    Console.WriteLine("The Strings are equal when capitalized ? " +
        (String.Compare(szAsciiUpper, szAsciiLower, true)==0?"true":"false")
 );
}
// Null terminated ASCII characters in a simple char array
char charArray3[4] = {0x41,0x42,0x43,0x00};
char * pstr3 =  &charArray3[ 0 ];
String^ szAsciiUpper = gcnew String( pstr3 );
char charArray4[4] = {0x61,0x62,0x63,0x00};
char * pstr4 =  &charArray4[ 0 ];
String^ szAsciiLower = gcnew String( pstr4,0,sizeof(charArray4) );

// Prints "ABC abc"
Console::WriteLine( String::Concat( szAsciiUpper,  " ", szAsciiLower )
 );

// Compare Strings - the result is true
Console::WriteLine( String::Concat(  "The Strings are equal when capitalized
 ? ", (0 == String::Compare( szAsciiUpper->ToUpper(), szAsciiLower->ToUpper()
 ) ? (String^)"TRUE" :  "FALSE") ) );

// This is the effective equivalent of another Compare method, which
 ignores case
Console::WriteLine( String::Concat(  "The Strings are equal when capitalized
 ? ", (0 == String::Compare( szAsciiUpper, szAsciiLower, true
 ) ? (String^)"TRUE" :  "FALSE") ) );
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「String.Compare メソッド」の関連用語

String.Compare メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



String.Compare メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS