英語で読む

次の方法で共有


String.CompareOrdinal メソッド

定義

それぞれの文字列の対応する String オブジェクトの数値を評価することで、2 つの Char を比較します。

オーバーロード

CompareOrdinal(String, String)

それぞれの文字列の対応する String オブジェクトの数値を評価することで、指定した 2 つの Char を比較します。

CompareOrdinal(String, Int32, String, Int32, Int32)

それぞれの部分文字列の対応する Char オブジェクトの数値を評価することにより、指定した 2 つの String オブジェクトの部分文字列を比較します。

CompareOrdinal(String, String)

ソース:
String.Comparison.cs
ソース:
String.Comparison.cs
ソース:
String.Comparison.cs

それぞれの文字列の対応する String オブジェクトの数値を評価することで、指定した 2 つの Char を比較します。

C#
public static int CompareOrdinal(string strA, string strB);
C#
public static int CompareOrdinal(string? strA, string? strB);

パラメーター

strA
String

比較する最初の文字列。

strB
String

比較する 2 番目の文字列。

戻り値

2 つの比較対照値の構文上の関係を示す整数。

[値] 条件
0 より小さい値 strAstrB より小さい値です。
ゼロ strAstrB が等しい。
0 より大きい値 strAstrB より大きくなっています。

次の例では、大文字と小文字のみが異なる 2 つの文字列の序数比較を実行します。

C#
// Sample for String.CompareOrdinal(String, String)
using System;

class Sample {
    public static void Main() {
    String str1 = "ABCD";
    String str2 = "abcd";
    String str;
    int result;

    Console.WriteLine();
    Console.WriteLine("Compare the numeric values of the corresponding Char objects in each string.");
    Console.WriteLine("str1 = '{0}', str2 = '{1}'", str1, str2);
    result = String.CompareOrdinal(str1, str2);
    str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to"));
    Console.Write("String '{0}' is ", str1);
    Console.Write("{0} ", str);
    Console.WriteLine("String '{0}'.", str2);
    }
}
/*
This example produces the following results:

Compare the numeric values of the corresponding Char objects in each string.
str1 = 'ABCD', str2 = 'abcd'
String 'ABCD' is less than String 'abcd'.
*/

注釈

このメソッドは、序数の並べ替え規則を使用して、大文字と小文字を区別する比較を実行します。 単語、文字列、および序数の並べ替えの詳細については、「」を参照してください System.Globalization.CompareOptions。 序数並べ替え規則を使用して大文字と小文字を区別しない比較を実行するには、 引数を にStringComparison.OrdinalIgnoreCase設定して Compare(String, String, StringComparison) メソッドをcomparisonType呼び出します。

は静的メソッドであり、 strBstrA は である可能性があるためCompareOrdinal(String, String)ですnull。 両方の値が の場合、メソッドは null0 (ゼロ) を返します。これは、 と strBstrA等しいことを示します。 値の 1 つだけが の場合、メソッドは nullnull 以外の値を大きくすると見なします。

こちらもご覧ください

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

CompareOrdinal(String, Int32, String, Int32, Int32)

ソース:
String.Comparison.cs
ソース:
String.Comparison.cs
ソース:
String.Comparison.cs

それぞれの部分文字列の対応する Char オブジェクトの数値を評価することにより、指定した 2 つの String オブジェクトの部分文字列を比較します。

C#
public static int CompareOrdinal(string strA, int indexA, string strB, int indexB, int length);
C#
public static int CompareOrdinal(string? strA, int indexA, string? strB, int indexB, int length);

パラメーター

strA
String

比較で使用する最初の文字列。

indexA
Int32

strA 内の部分文字列の開始インデックス。

strB
String

比較で使用する 2 番目の文字列。

indexB
Int32

strB 内の部分文字列の開始インデックス。

length
Int32

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

戻り値

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

[値] 条件
0 より小さい値 strA 内の部分文字列が strB 内の部分文字列より小さいです。
ゼロ これらの部分文字列が等しいか、または length が 0 です。
0 より大きい値 strA 内の部分文字列が strB 内の部分文字列より大きいです。

例外

strAnull でありません。また indexAstrA.Lengthを超えています。

または

strBnull でありません。また indexBstrB.Lengthを超えています。

または

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

次の例では、 と CompareCompareOrdinal異なる並べ替え順序を使用する方法を示します。

C#
using System;
using System.Globalization;

class Test
{
    public static void Main(String[] args)
    {
    String strLow = "abc";
    String strCap = "ABC";
    String result = "equal to ";
    int x = 0;
    int pos = 1;

// The Unicode codepoint for 'b' is greater than the codepoint for 'B'.
    x = String.CompareOrdinal(strLow, pos, strCap, pos, 1);
    if (x < 0) result = "less than";
    if (x > 0) result = "greater than";
    Console.WriteLine("CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
    Console.WriteLine("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);

// In U.S. English culture, 'b' is linguistically less than 'B'.
    x = String.Compare(strLow, pos, strCap, pos, 1, false, new CultureInfo("en-US"));
    if (x < 0) result = "less than";
    else if (x > 0) result = "greater than";
    Console.WriteLine("Compare(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, pos);
    Console.WriteLine("   '{0}' is {1} '{2}'", strLow[pos], result, strCap[pos]);
    }
}

注釈

indexB、および length パラメーターはindexA負でない必要があります。

比較される文字数は、 の長さがstrA小さい、長さが少ないindexAindexBstrBおよび lengthです。

このメソッドは、序数の並べ替え規則を使用して、大文字と小文字を区別する比較を実行します。 単語、文字列、および序数の並べ替えの詳細については、「」を参照してください System.Globalization.CompareOptions。 序数並べ替え規則を使用して大文字と小文字を区別しない比較を実行するには、 引数を にStringComparison.OrdinalIgnoreCase設定して Compare(String, Int32, String, Int32, Int32, StringComparison) メソッドをcomparisonType呼び出します。

は静的メソッドであり、 strBstrA は である可能性があるためCompareOrdinal(String, String)ですnull。 両方の値が の場合、メソッドは null0 (ゼロ) を返します。これは、 と strBstrA等しいことを示します。 値の 1 つだけが の場合、メソッドは nullnull 以外の値を大きくすると見なします。

こちらもご覧ください

適用対象

.NET 10 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.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
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0