String.CompareOrdinal String.CompareOrdinal String.CompareOrdinal String.CompareOrdinal Method

定義

それぞれの文字列の対応する String オブジェクトの数値を評価することで、2 つの Char を比較します。Compares two String objects by evaluating the numeric values of the corresponding Char objects in each string.

オーバーロード

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

それぞれの文字列の対応する String オブジェクトの数値を評価することで、指定した 2 つの Char を比較します。Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string.

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

それぞれの部分文字列の対応する Char オブジェクトの数値を評価することにより、指定した 2 つの String オブジェクトの部分文字列を比較します。Compares substrings of two specified String objects by evaluating the numeric values of the corresponding Char objects in each substring.

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

それぞれの文字列の対応する String オブジェクトの数値を評価することで、指定した 2 つの Char を比較します。Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string.

public:
 static int CompareOrdinal(System::String ^ strA, System::String ^ strB);
public static int CompareOrdinal (string strA, string strB);
static member CompareOrdinal : string * string -> int
Public Shared Function CompareOrdinal (strA As String, strB As String) As Integer

パラメーター

strA
String String String String

比較する最初の文字列。The first string to compare.

strB
String String String String

比較する 2 番目の文字列。The second string to compare.

戻り値

2 つの比較対照値の構文上の関係を示す整数。An integer that indicates the lexical relationship between the two comparands.

[値]Value 条件Condition
0 より小さい値Less than zero strAstrB より小さい値です。strA is less than strB.
0Zero strAstrB が等しい。strA and strB are equal.
0 より大きい値Greater than zero strAstrB より大きくなっています。strA is greater than strB.

次の例を実行し、2 つの序数に基づく比較文字列の場合のみが異なります。The following example performs and ordinal comparison of two strings that only differ in case.

// Sample for String::CompareOrdinal(String, String)
using namespace System;
int 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) ? (String^)"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'.
*/
// 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'.
*/
' Sample for String.CompareOrdinal(String, String)
Imports System
Imports Microsoft.VisualBasic

Class Sample
   Public Shared Sub Main()
      Dim str1 As [String] = "ABCD"
      Dim str2 As [String] = "abcd"
      Dim str As [String]
      Dim result As Integer
      
      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 = IIf(result < 0, "less than", IIf(result > 0, "greater than", "equal to"))
      Console.Write("String '{0}' is ", str1)
      Console.Write("{0} ", str)
      Console.WriteLine("String '{0}'.", str2)
   End Sub 'Main
End Class 'Sample
'
'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'.
'

注釈

このメソッドは、序数の並べ替え規則を使用して、大文字小文字の比較を実行します。This method performs a case-sensitive comparison using ordinal sort rules. 単語、文字列、および序数の並べ替えの詳細については、次を参照してください。System.Globalization.CompareOptionsします。For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions. 序数の並べ替え規則を使用して、区別しない比較を実行するには、呼び出し、Compare(String, String, StringComparison)メソッドをcomparisonType引数に設定StringComparison.OrdinalIgnoreCaseします。To perform a case-insensitive comparison using ordinal sort rules, call the Compare(String, String, StringComparison) method with the comparisonType argument set to StringComparison.OrdinalIgnoreCase.

CompareOrdinal(String, String)静的メソッドでは、strAstrBできるnullします。Because CompareOrdinal(String, String) is a static method, strA and strB can be null. 両方の値が場合null、ことを示す 0 (ゼロ) が返されますstrAstrBが等しい。If both values are null, the method returns 0 (zero), which indicates that strA and strB are equal. 値の 1 つだけ場合nullメソッドが null 以外の値を大きくすると見なします。If only one of the values is null, the method considers the non-null value to be greater.

こちらもご覧ください

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

それぞれの部分文字列の対応する Char オブジェクトの数値を評価することにより、指定した 2 つの String オブジェクトの部分文字列を比較します。Compares substrings of two specified String objects by evaluating the numeric values of the corresponding Char objects in each substring.

public:
 static int CompareOrdinal(System::String ^ strA, int indexA, System::String ^ strB, int indexB, int length);
public static int CompareOrdinal (string strA, int indexA, string strB, int indexB, int length);
static member CompareOrdinal : string * int * string * int * int -> int
Public Shared Function CompareOrdinal (strA As String, indexA As Integer, strB As String, indexB As Integer, length As Integer) As Integer

パラメーター

strA
String String String String

比較で使用する最初の文字列。The first string to use in the comparison.

indexA
Int32 Int32 Int32 Int32

strA 内の部分文字列の開始インデックス。The starting index of the substring in strA.

strB
String String String String

比較で使用する 2 番目の文字列。The second string to use in the comparison.

indexB
Int32 Int32 Int32 Int32

strB 内の部分文字列の開始インデックス。The starting index of the substring in strB.

length
Int32 Int32 Int32 Int32

比較する各部分文字列の最大文字数。The maximum number of characters in the substrings to compare.

戻り値

2 つの比較対照値の構文上の関係を示す 32 ビット符号付き整数。A 32-bit signed integer that indicates the lexical relationship between the two comparands.

[値]Value 条件Condition
0 より小さい値Less than zero strA 内の部分文字列が strB 内の部分文字列より小さいです。The substring in strA is less than the substring in strB.
0Zero これらの部分文字列が等しいか、または length が 0 です。The substrings are equal, or length is zero.
0 より大きい値Greater than zero strA 内の部分文字列が strB 内の部分文字列より大きいです。The substring in strA is greater than the substring in strB.

例外

strAnull でありません。また indexAstrA.Length を超えています。strA is not null and indexA is greater than strA.Length.

または-or- strBnull でありません。また indexBstrB.Lengthを超えています。strB is not null and indexB is greater than strB.Length.

- または --or- indexAindexB、または length が負の値です。indexA, indexB, or length is negative.

次の例を示しているCompareOrdinalCompareさまざまな並べ替え順序を使用します。This following example demonstrates that CompareOrdinal and Compare use different sort orders.

using namespace System;
using namespace System::Globalization;
int main()
{
   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, gcnew 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 ] );
}
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]);
	}
}
Imports System
Imports System.Globalization

Class Test
   
  Public Shared Sub Main(args() As [String])
      Dim strLow As [String] = "abc"
      Dim strCap As [String] = "ABC"
      Dim result As [String] = "equal to "
      Dim x As Integer = 0
      Dim pos As Integer = 1

' The Unicode codepoint for 'b' is greater than the codepoint for 'B'.      
      x = [String].CompareOrdinal(strLow, pos, strCap, pos, 1)
      If x < 0 Then
         result = "less than"
      End If
      If x > 0 Then
         result = "greater than"
      End If

' In U.S. English culture, 'b' is linguistically less than 'B'.
      Console.WriteLine("CompareOrdinal(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos)
      
      Console.WriteLine("   '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos))
      
      x = [String].Compare(strLow, pos, strCap, pos, 1, False, New CultureInfo("en-US"))
      If x < 0 Then
         result = "less than"
      ElseIf x > 0 Then
         result = "greater than"
      End If
      Console.WriteLine("Compare(""{0}"".Chars({2}), ""{1}"".Chars({2})):", strLow, strCap, pos)
      Console.WriteLine("   '{0}' is {1} '{2}'", strLow.Chars(pos), result, strCap.Chars(pos))
   End Sub 'Main
End Class 'Test

注釈

indexAindexB、およびlengthパラメーターは、0 以上である必要があります。The indexA, indexB, and length parameters must be nonnegative.

比較する文字の数が小さい方の長さのstrA少ないindexAの長さstrB少ないindexB、およびlengthします。The number of characters compared is the lesser of the length of strA less indexA, the length of strB less indexB, and length.

このメソッドは、序数の並べ替え規則を使用して、大文字小文字の比較を実行します。This method performs a case-sensitive comparison using ordinal sort rules. 単語、文字列、および序数の並べ替えの詳細については、次を参照してください。System.Globalization.CompareOptionsします。For more information about word, string, and ordinal sorts, see System.Globalization.CompareOptions. 序数の並べ替え規則を使用して、区別しない比較を実行するには、呼び出し、Compare(String, Int32, String, Int32, Int32, StringComparison)メソッドをcomparisonType引数に設定StringComparison.OrdinalIgnoreCaseします。To perform a case-insensitive comparison using ordinal sort rules, call the Compare(String, Int32, String, Int32, Int32, StringComparison) method with the comparisonType argument set to StringComparison.OrdinalIgnoreCase.

CompareOrdinal(String, String)静的メソッドでは、strAstrBできるnullします。Because CompareOrdinal(String, String) is a static method, strA and strB can be null. 両方の値が場合null、ことを示す 0 (ゼロ) が返されますstrAstrBが等しい。If both values are null, the method returns 0 (zero), which indicates that strA and strB are equal. 値の 1 つだけ場合nullメソッドが null 以外の値を大きくすると見なします。If only one of the values is null, the method considers the non-null value to be greater.

こちらもご覧ください

適用対象