次の方法で共有


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

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

Overloads Public Shared Function Compare( _
   ByVal strA As String, _   ByVal strB As String _) As Integer
[C#]
public static int Compare(stringstrA,stringstrB);
[C++]
public: static int Compare(String* strA,String* strB);
[JScript]
public static function Compare(
   strA : String,strB : String) : int;

パラメータ

戻り値

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

パスの名前は変更できない方法で比較する必要があります。これを実行するための適切なコードは次のとおりです。

static String IsFileURI(String path) { 
   return (String.Compare(path, 0, "file:", 0, 5, true, CultureInfo.InvariantCulture)== 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


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

   }
}


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

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

__gc class ReverseStringComparer : public IComparer {
public:
    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 (S"{0,10}: ", title);
    StringBuilder* sb = new 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 = new ArrayList();
    myAL->Add(S"Eric");
    myAL->Add(S"Mark");
    myAL->Add(S"Lance");
    myAL->Add(S"Rob");
    myAL->Add(S"Kris");
    myAL->Add(S"Brad");
    myAL->Add(S"Kit");
    myAL->Add(S"Bradley");
    myAL->Add(S"Keith");
    myAL->Add(S"Susan");

    // Displays the properties and values of the ArrayList.
    Console::WriteLine(S"Count: {0}", myAL->Count.ToString());
    
    PrintValues(S"Unsorted", myAL);
    myAL->Sort();
    PrintValues(S"Sorted", myAL);
    myAL->Sort(new ReverseStringComparer());
    PrintValues(S"Reverse", myAL);

    String* names[] = dynamic_cast<String*[]>(myAL->ToArray(__typeof(String)));
}

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

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

String クラス | String メンバ | System 名前空間 | String.Compare オーバーロードの一覧 | Int32 | CompareOrdinal | CompareTo