Share via


String.CompareOrdinal メソッド

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

オーバーロードの一覧

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

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function CompareOrdinal(String, String) As Integer

[C#] public static int CompareOrdinal(string, string);

[C++] public: static int CompareOrdinal(String*, String*);

[JScript] public static function CompareOrdinal(String, String) : int;

それぞれの部分文字列の対応する Char オブジェクトの数値を評価することで、指定した 2 つの String を比較します。パラメータには、部分文字列の長さと開始位置を指定します。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function CompareOrdinal(String, Integer, String, Integer, Integer) As Integer

[C#] public static int CompareOrdinal(string, int, string, int, int);

[C++] public: static int CompareOrdinal(String*, int, String*, int, int);

[JScript] public static function CompareOrdinal(String, int, String, int, int) : int;

使用例

[Visual Basic, C#, C++] CompareOrdinalCompare が異なる並べ替え順序を使用することを、次の例に示します。

[Visual Basic, C#, C++] メモ   ここでは、CompareOrdinal のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
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

[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]);
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Globalization;

int main()
{
   String* strLow = S"abc";
   String* strCap = S"ABC";
   String* result = S"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 = S"less than";
   if (x > 0) result = S"greater than";
   Console::WriteLine(S"CompareOrdinal(\"{0}\"[{2}], \"{1}\"[{2}]):", strLow, strCap, __box(pos));
   Console::WriteLine(S"   '{0}' is {1} '{2}'", __box(strLow->Chars[pos]), result, __box(strCap->Chars[pos]));

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

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

String クラス | String メンバ | System 名前空間