String.CompareOrdinalとは? わかりやすく解説

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

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

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

Public Shared Function CompareOrdinal
 ( _
    strA As String, _
    strB As String _
) As Integer
public static int CompareOrdinal
 (
    string strA,
    string strB
)
public:
static int CompareOrdinal (
    String^ strA, 
    String^ strB
)
public static int CompareOrdinal
 (
    String strA, 
    String strB
)
public static function CompareOrdinal
 (
    strA : String, 
    strB : String
) : int

パラメータ

strA

第 1 の String

strB

第 2 の String

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

条件

0 より小さい値

strAstrB より小さい。

0

strAstrB等しい。

0 より大きい

strAstrB より大きい

解説解説
使用例使用例

大文字と小文字表記だけが異な2 つ文字列について、序数ベース比較実行するコード例次に示します

' 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'.
'
// 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)
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)
import System.*;

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

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

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

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

Public Shared Function CompareOrdinal
 ( _
    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.CompareOrdinal(strA, indexA, strB, indexB,
 length)
public static int CompareOrdinal
 (
    string strA,
    int indexA,
    string strB,
    int indexB,
    int length
)
public:
static int CompareOrdinal (
    String^ strA, 
    int indexA, 
    String^ strB, 
    int indexB, 
    int length
)
public static int CompareOrdinal
 (
    String strA, 
    int indexA, 
    String strB, 
    int indexB, 
    int length
)
public static function CompareOrdinal
 (
    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 が負の値です。

解説解説

indexAindexB、および length の各パラメータ負数以外である必要があります

比較される文字数は、strA から indexA引いた長さstrB から indexB引いた長さ、および length のうちで一番小さい値になります

このメソッドは、序数並べ替え規則使用して大文字と小文字区別する比較実行します単語文字列序数並べ替え詳細については、「System.Globalization.CompareOptions」を参照してください

使用例使用例

CompareOrdinal と Compare異な並べ替え順序使用することを、次のコード例示します

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
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]);
    }
}
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 ] );
}
import System.*;
import 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, (Int32)pos);
        Console.WriteLine("   '{0}' is {1} '{2}'", 
            (System.Char)strLow.charAt(pos), result, 
            (System.Char)strCap.charAt(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, (Int32)pos);
        Console.WriteLine("   '{0}' is {1} '{2}'", 
            (System.Char)strLow.charAt(pos), result, 
            (System.Char)strCap.charAt(pos));
    } //main
} //Test
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.CompareOrdinal メソッド




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

辞書ショートカット

すべての辞書の索引

「String.CompareOrdinal」の関連用語

String.CompareOrdinalのお隣キーワード
検索ランキング

   

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



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

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

©2025 GRAS Group, Inc.RSS