String.Equals Metoda

Definicja

Określa, czy dwa String obiekty mają tę samą wartość.

Przeciążenia

Equals(Object)

Określa, czy to wystąpienie i określony obiekt, który również musi być obiektem String , mają tę samą wartość.

Equals(String)

Określa, czy to wystąpienie i inny określony String obiekt mają tę samą wartość.

Equals(String, String)

Określa, czy dwa określone String obiekty mają tę samą wartość.

Equals(String, StringComparison)

Określa, czy ten ciąg i określony String obiekt mają taką samą wartość. Parametr określa kulturę, wielkość liter i reguły sortowania używane w porównaniu.

Equals(String, String, StringComparison)

Określa, czy dwa określone String obiekty mają tę samą wartość. Parametr określa kulturę, wielkość liter i reguły sortowania używane w porównaniu.

Equals(Object)

Źródło:
String.Comparison.cs
Źródło:
String.Comparison.cs
Źródło:
String.Comparison.cs

Określa, czy to wystąpienie i określony obiekt, który również musi być obiektem String , mają tę samą wartość.

public:
 override bool Equals(System::Object ^ obj);
public override bool Equals (object obj);
public override bool Equals (object? obj);
override this.Equals : obj -> bool
Public Overrides Function Equals (obj As Object) As Boolean

Parametry

obj
Object

Ciąg do porównania z tym wystąpieniem.

Zwraca

true jeśli obj parametr ma wartość , String a jego wartość jest taka sama jak to wystąpienie; w przeciwnym razie wartość false. Jeśli obj parametr ma nullwartość , metoda zwraca falsewartość .

Przykłady

W poniższym przykładzie pokazano metodę Equals .

// Sample for String::Equals(Object)
//            String::Equals(String)
//            String::Equals(String, String)
using namespace System;
using namespace System::Text;
int main()
{
   StringBuilder^ sb = gcnew StringBuilder( "abcd" );
   String^ str1 = "abcd";
   String^ str2 = nullptr;
   Object^ o2 = nullptr;
   Console::WriteLine();
   Console::WriteLine( " *  The value of String str1 is '{0}'.", str1 );
   Console::WriteLine( " *  The value of StringBuilder sb is '{0}'.", sb );
   Console::WriteLine();
   Console::WriteLine( "1a) String::Equals(Object). Object is a StringBuilder, not a String." );
   Console::WriteLine( "    Is str1 equal to sb?: {0}", str1->Equals( sb ) );
   Console::WriteLine();
   Console::WriteLine( "1b) String::Equals(Object). Object is a String." );
   str2 = sb->ToString();
   o2 = str2;
   Console::WriteLine( " *  The value of Object o2 is '{0}'.", o2 );
   Console::WriteLine( "    Is str1 equal to o2?: {0}", str1->Equals( o2 ) );
   Console::WriteLine();
   Console::WriteLine( " 2) String::Equals(String)" );
   Console::WriteLine( " *  The value of String str2 is '{0}'.", str2 );
   Console::WriteLine( "    Is str1 equal to str2?: {0}", str1->Equals( str2 ) );
   Console::WriteLine();
   Console::WriteLine( " 3) String::Equals(String, String)" );
   Console::WriteLine( "    Is str1 equal to str2?: {0}", String::Equals( str1, str2 ) );
}

/*
This example produces the following results:

 *  The value of String str1 is 'abcd'.
 *  The value of StringBuilder sb is 'abcd'.

1a) String::Equals(Object). Object is a StringBuilder, not a String.
    Is str1 equal to sb?: False

1b) String::Equals(Object). Object is a String.
 *  The value of Object o2 is 'abcd'.
    Is str1 equal to o2?: True

 2) String::Equals(String)
 *  The value of String str2 is 'abcd'.
    Is str1 equal to str2?: True

 3) String::Equals(String, String)
    Is str1 equal to str2?: True
*/
// Sample for String.Equals(Object)
//            String.Equals(String)
//            String.Equals(String, String)
using System;
using System.Text;

class Sample1
{
    public static void Main()
    {
        StringBuilder sb = new StringBuilder("abcd");
        String str1 = "abcd";
        String str2 = null;
        Object o2 = null;

        Console.WriteLine();
        Console.WriteLine(" *  The value of String str1 is '{0}'.", str1);
        Console.WriteLine(" *  The value of StringBuilder sb is '{0}'.", sb.ToString());

        Console.WriteLine();
        Console.WriteLine("1a) String.Equals(Object). Object is a StringBuilder, not a String.");
        Console.WriteLine("    Is str1 equal to sb?: {0}", str1.Equals(sb));

        Console.WriteLine();
        Console.WriteLine("1b) String.Equals(Object). Object is a String.");
        str2 = sb.ToString();
        o2 = str2;
        Console.WriteLine(" *  The value of Object o2 is '{0}'.", o2);
        Console.WriteLine("    Is str1 equal to o2?: {0}", str1.Equals(o2));

        Console.WriteLine();
        Console.WriteLine(" 2) String.Equals(String)");
        Console.WriteLine(" *  The value of String str2 is '{0}'.", str2);
        Console.WriteLine("    Is str1 equal to str2?: {0}", str1.Equals(str2));

        Console.WriteLine();
        Console.WriteLine(" 3) String.Equals(String, String)");
        Console.WriteLine("    Is str1 equal to str2?: {0}", String.Equals(str1, str2));
    }
}
/*
This example produces the following results:

 *  The value of String str1 is 'abcd'.
 *  The value of StringBuilder sb is 'abcd'.

1a) String.Equals(Object). Object is a StringBuilder, not a String.
    Is str1 equal to sb?: False

1b) String.Equals(Object). Object is a String.
 *  The value of Object o2 is 'abcd'.
    Is str1 equal to o2?: True

 2) String.Equals(String)
 *  The value of String str2 is 'abcd'.
    Is str1 equal to str2?: True

 3) String.Equals(String, String)
    Is str1 equal to str2?: True
*/
// Sample for String.Equals(Object)
//            String.Equals(String)
//            String.Equals(String, String)
open System
open System.Text

let sb = StringBuilder "abcd"
let str1 = "abcd"
let str2 = string sb
let o2: obj = str2

printfn $"""
*  The value of String str1 is '{str1}'.
*  The value of StringBuilder sb is '{sb}'.

1a) String.Equals(Object). Object is a StringBuilder, not a String.
Is str1 equal to sb?: {str1.Equals sb}"

1b) String.Equals(Object). Object is a String.
*  The value of Object o2 is '{o2}'.
Is str1 equal to o2?: {str1.Equals o2}

2) String.Equals(String)
*  The value of String str2 is '{str2}'.
Is str1 equal to str2?: {str1.Equals str2}

3) String.Equals(String, String)
Is str1 equal to str2?: {String.Equals(str1, str2)}"""
(*
This example produces the following results:

 *  The value of String str1 is 'abcd'.
 *  The value of StringBuilder sb is 'abcd'.

1a) String.Equals(Object). Object is a StringBuilder, not a String.
    Is str1 equal to sb?: False

1b) String.Equals(Object). Object is a String.
 *  The value of Object o2 is 'abcd'.
    Is str1 equal to o2?: True

 2) String.Equals(String)
 *  The value of String str2 is 'abcd'.
    Is str1 equal to str2?: True

 3) String.Equals(String, String)
    Is str1 equal to str2?: True
*)
' Sample for String.Equals(Object)
'            String.Equals(String)
'            String.Equals(String, String)
Imports System.Text

Class Sample
   Public Shared Sub Main()
      Dim sb As New StringBuilder("abcd")
      Dim str1 As [String] = "abcd"
      Dim str2 As [String] = Nothing
      Dim o2 As [Object] = Nothing
      
      Console.WriteLine()
      Console.WriteLine(" *  The value of String str1 is '{0}'.", str1)
      Console.WriteLine(" *  The value of StringBuilder sb is '{0}'.", sb.ToString())
      
      Console.WriteLine()
      Console.WriteLine("1a) String.Equals(Object). Object is a StringBuilder, not a String.")
      Console.WriteLine("    Is str1 equal to sb?: {0}", str1.Equals(sb))
      
      Console.WriteLine()
      Console.WriteLine("1b) String.Equals(Object). Object is a String.")
      str2 = sb.ToString()
      o2 = str2
      Console.WriteLine(" *  The value of Object o2 is '{0}'.", o2)
      Console.WriteLine("    Is str1 equal to o2?: {0}", str1.Equals(o2))
      
      Console.WriteLine()
      Console.WriteLine(" 2) String.Equals(String)")
      Console.WriteLine(" *  The value of String str2 is '{0}'.", str2)
      Console.WriteLine("    Is str1 equal to str2?: {0}", str1.Equals(str2))
      
      Console.WriteLine()
      Console.WriteLine(" 3) String.Equals(String, String)")
      Console.WriteLine("    Is str1 equal to str2?: {0}", [String].Equals(str1, str2))
   End Sub
End Class
'
'This example produces the following results:
'
' *  The value of String str1 is 'abcd'.
' *  The value of StringBuilder sb is 'abcd'.
'
'1a) String.Equals(Object). Object is a StringBuilder, not a String.
'    Is str1 equal to sb?: False
'
'1b) String.Equals(Object). Object is a String.
' *  The value of Object o2 is 'abcd'.
'    Is str1 equal to o2?: True
'
' 2) String.Equals(String)
' *  The value of String str2 is 'abcd'.
'    Is str1 equal to str2?: True
'
' 3) String.Equals(String, String)
'    Is str1 equal to str2?: True
'

Uwagi

Ta metoda wykonuje porównanie porządkowe (wielkość liter i niewrażliwość na ustawienia kulturowe).

Zobacz też

Dotyczy

Equals(String)

Źródło:
String.Comparison.cs
Źródło:
String.Comparison.cs
Źródło:
String.Comparison.cs

Określa, czy to wystąpienie i inny określony String obiekt mają tę samą wartość.

public:
 virtual bool Equals(System::String ^ value);
public:
 bool Equals(System::String ^ value);
public bool Equals (string value);
public bool Equals (string? value);
override this.Equals : string -> bool
Public Function Equals (value As String) As Boolean

Parametry

value
String

Ciąg do porównania z tym wystąpieniem.

Zwraca

truejeśli wartość parametru value jest taka sama jak wartość tego wystąpienia; w przeciwnym razie . false Jeśli value parametr ma nullwartość , metoda zwraca falsewartość .

Implementuje

Przykłady

W poniższym przykładzie pokazano metodę Equals . Porównuje tytułowy wyraz "File" z równoważnym słowem, jego małymi literami odpowiednik, jego wielką literą i wyrazem zawierającym ALFABET ŁACIŃSKI SMALL LETTER DOTLESS I (U+0131) zamiast LATIN SMALL LETTER I (U+0069). Equals(String) Ponieważ metoda wykonuje porównanie porządkowe, tylko porównanie z identycznym wyrazem zwraca wartość true.

using System;

public class Example
{
    public static void Main()
    {
        Console.OutputEncoding = System.Text.Encoding.UTF8;
        string word = "File";
        string[] others = { word.ToLower(), word, word.ToUpper(), "Fıle" };
        foreach (string other in others)
        {
            if (word.Equals(other))
                Console.WriteLine("{0} = {1}", word, other);
            else
                Console.WriteLine("{0} {1} {2}", word, '\u2260', other);
        }
    }
}
// The example displays the following output:
//       File ≠ file
//       File = File
//       File ≠ FILE
//       File ≠ Fıle
open System

Console.OutputEncoding <- Text.Encoding.UTF8
let word = "File"
let others = [| word.ToLower(); word; word.ToUpper(); "Fıle" |]
for other in others do
    if word.Equals other then
        printfn $"{word} = {other}"
    else
        printfn $"{word} \u2260 {other}"
// The example displays the following output:
//       File ≠ file
//       File = File
//       File ≠ FILE
//       File ≠ Fıle
Module Example
   Public Sub Main()
      Console.OutputEncoding = System.Text.Encoding.UTF8
      
      Dim word As String = "File"
      Dim others() As String = { word.ToLower(), word, word.ToUpper(), _
                                 "Fıle" }
      For Each other As String In others
         If word.Equals(other) Then 
            Console.WriteLine("{0} = {1}", word, other)
         Else
            Console.WriteLine("{0} {1} {2}", word, ChrW(&H2260), other)
         End If      
      Next        
   End Sub
End Module
' The example displays the following output:
'       File ≠ file
'       File = File
'       File ≠ FILE
'       File ≠ Fıle

Uwagi

Ta metoda wykonuje porównanie porządkowe (wielkość liter i niewrażliwość na ustawienia kulturowe).

Zobacz też

Dotyczy

Equals(String, String)

Źródło:
String.Comparison.cs
Źródło:
String.Comparison.cs
Źródło:
String.Comparison.cs

Określa, czy dwa określone String obiekty mają tę samą wartość.

public:
 static bool Equals(System::String ^ a, System::String ^ b);
public static bool Equals (string a, string b);
public static bool Equals (string? a, string? b);
static member Equals : string * string -> bool
Public Shared Function Equals (a As String, b As String) As Boolean

Parametry

a
String

Pierwszy ciąg do porównania lub null.

b
String

Drugi ciąg do porównania lub null.

Zwraca

true jeśli wartość a jest taka sama jak wartość b; w przeciwnym razie false. Jeśli obie a metody i bnull, metoda zwraca wartość true.

Przykłady

W poniższym przykładzie pokazano metodę Equals .

// Sample for String::Equals(Object)
//            String::Equals(String)
//            String::Equals(String, String)
using namespace System;
using namespace System::Text;
int main()
{
   StringBuilder^ sb = gcnew StringBuilder( "abcd" );
   String^ str1 = "abcd";
   String^ str2 = nullptr;
   Object^ o2 = nullptr;
   Console::WriteLine();
   Console::WriteLine( " *  The value of String str1 is '{0}'.", str1 );
   Console::WriteLine( " *  The value of StringBuilder sb is '{0}'.", sb );
   Console::WriteLine();
   Console::WriteLine( "1a) String::Equals(Object). Object is a StringBuilder, not a String." );
   Console::WriteLine( "    Is str1 equal to sb?: {0}", str1->Equals( sb ) );
   Console::WriteLine();
   Console::WriteLine( "1b) String::Equals(Object). Object is a String." );
   str2 = sb->ToString();
   o2 = str2;
   Console::WriteLine( " *  The value of Object o2 is '{0}'.", o2 );
   Console::WriteLine( "    Is str1 equal to o2?: {0}", str1->Equals( o2 ) );
   Console::WriteLine();
   Console::WriteLine( " 2) String::Equals(String)" );
   Console::WriteLine( " *  The value of String str2 is '{0}'.", str2 );
   Console::WriteLine( "    Is str1 equal to str2?: {0}", str1->Equals( str2 ) );
   Console::WriteLine();
   Console::WriteLine( " 3) String::Equals(String, String)" );
   Console::WriteLine( "    Is str1 equal to str2?: {0}", String::Equals( str1, str2 ) );
}

/*
This example produces the following results:

 *  The value of String str1 is 'abcd'.
 *  The value of StringBuilder sb is 'abcd'.

1a) String::Equals(Object). Object is a StringBuilder, not a String.
    Is str1 equal to sb?: False

1b) String::Equals(Object). Object is a String.
 *  The value of Object o2 is 'abcd'.
    Is str1 equal to o2?: True

 2) String::Equals(String)
 *  The value of String str2 is 'abcd'.
    Is str1 equal to str2?: True

 3) String::Equals(String, String)
    Is str1 equal to str2?: True
*/
// Sample for String.Equals(Object)
//            String.Equals(String)
//            String.Equals(String, String)
using System;
using System.Text;

class Sample1
{
    public static void Main()
    {
        StringBuilder sb = new StringBuilder("abcd");
        String str1 = "abcd";
        String str2 = null;
        Object o2 = null;

        Console.WriteLine();
        Console.WriteLine(" *  The value of String str1 is '{0}'.", str1);
        Console.WriteLine(" *  The value of StringBuilder sb is '{0}'.", sb.ToString());

        Console.WriteLine();
        Console.WriteLine("1a) String.Equals(Object). Object is a StringBuilder, not a String.");
        Console.WriteLine("    Is str1 equal to sb?: {0}", str1.Equals(sb));

        Console.WriteLine();
        Console.WriteLine("1b) String.Equals(Object). Object is a String.");
        str2 = sb.ToString();
        o2 = str2;
        Console.WriteLine(" *  The value of Object o2 is '{0}'.", o2);
        Console.WriteLine("    Is str1 equal to o2?: {0}", str1.Equals(o2));

        Console.WriteLine();
        Console.WriteLine(" 2) String.Equals(String)");
        Console.WriteLine(" *  The value of String str2 is '{0}'.", str2);
        Console.WriteLine("    Is str1 equal to str2?: {0}", str1.Equals(str2));

        Console.WriteLine();
        Console.WriteLine(" 3) String.Equals(String, String)");
        Console.WriteLine("    Is str1 equal to str2?: {0}", String.Equals(str1, str2));
    }
}
/*
This example produces the following results:

 *  The value of String str1 is 'abcd'.
 *  The value of StringBuilder sb is 'abcd'.

1a) String.Equals(Object). Object is a StringBuilder, not a String.
    Is str1 equal to sb?: False

1b) String.Equals(Object). Object is a String.
 *  The value of Object o2 is 'abcd'.
    Is str1 equal to o2?: True

 2) String.Equals(String)
 *  The value of String str2 is 'abcd'.
    Is str1 equal to str2?: True

 3) String.Equals(String, String)
    Is str1 equal to str2?: True
*/
// Sample for String.Equals(Object)
//            String.Equals(String)
//            String.Equals(String, String)
open System
open System.Text

let sb = StringBuilder "abcd"
let str1 = "abcd"
let str2 = string sb
let o2: obj = str2

printfn $"""
*  The value of String str1 is '{str1}'.
*  The value of StringBuilder sb is '{sb}'.

1a) String.Equals(Object). Object is a StringBuilder, not a String.
Is str1 equal to sb?: {str1.Equals sb}"

1b) String.Equals(Object). Object is a String.
*  The value of Object o2 is '{o2}'.
Is str1 equal to o2?: {str1.Equals o2}

2) String.Equals(String)
*  The value of String str2 is '{str2}'.
Is str1 equal to str2?: {str1.Equals str2}

3) String.Equals(String, String)
Is str1 equal to str2?: {String.Equals(str1, str2)}"""
(*
This example produces the following results:

 *  The value of String str1 is 'abcd'.
 *  The value of StringBuilder sb is 'abcd'.

1a) String.Equals(Object). Object is a StringBuilder, not a String.
    Is str1 equal to sb?: False

1b) String.Equals(Object). Object is a String.
 *  The value of Object o2 is 'abcd'.
    Is str1 equal to o2?: True

 2) String.Equals(String)
 *  The value of String str2 is 'abcd'.
    Is str1 equal to str2?: True

 3) String.Equals(String, String)
    Is str1 equal to str2?: True
*)
' Sample for String.Equals(Object)
'            String.Equals(String)
'            String.Equals(String, String)
Imports System.Text

Class Sample
   Public Shared Sub Main()
      Dim sb As New StringBuilder("abcd")
      Dim str1 As [String] = "abcd"
      Dim str2 As [String] = Nothing
      Dim o2 As [Object] = Nothing
      
      Console.WriteLine()
      Console.WriteLine(" *  The value of String str1 is '{0}'.", str1)
      Console.WriteLine(" *  The value of StringBuilder sb is '{0}'.", sb.ToString())
      
      Console.WriteLine()
      Console.WriteLine("1a) String.Equals(Object). Object is a StringBuilder, not a String.")
      Console.WriteLine("    Is str1 equal to sb?: {0}", str1.Equals(sb))
      
      Console.WriteLine()
      Console.WriteLine("1b) String.Equals(Object). Object is a String.")
      str2 = sb.ToString()
      o2 = str2
      Console.WriteLine(" *  The value of Object o2 is '{0}'.", o2)
      Console.WriteLine("    Is str1 equal to o2?: {0}", str1.Equals(o2))
      
      Console.WriteLine()
      Console.WriteLine(" 2) String.Equals(String)")
      Console.WriteLine(" *  The value of String str2 is '{0}'.", str2)
      Console.WriteLine("    Is str1 equal to str2?: {0}", str1.Equals(str2))
      
      Console.WriteLine()
      Console.WriteLine(" 3) String.Equals(String, String)")
      Console.WriteLine("    Is str1 equal to str2?: {0}", [String].Equals(str1, str2))
   End Sub
End Class
'
'This example produces the following results:
'
' *  The value of String str1 is 'abcd'.
' *  The value of StringBuilder sb is 'abcd'.
'
'1a) String.Equals(Object). Object is a StringBuilder, not a String.
'    Is str1 equal to sb?: False
'
'1b) String.Equals(Object). Object is a String.
' *  The value of Object o2 is 'abcd'.
'    Is str1 equal to o2?: True
'
' 2) String.Equals(String)
' *  The value of String str2 is 'abcd'.
'    Is str1 equal to str2?: True
'
' 3) String.Equals(String, String)
'    Is str1 equal to str2?: True
'

Uwagi

Ta metoda wykonuje porównanie porządkowe (wielkość liter i niewrażliwość na ustawienia kulturowe).

Zobacz też

Dotyczy

Equals(String, StringComparison)

Źródło:
String.Comparison.cs
Źródło:
String.Comparison.cs
Źródło:
String.Comparison.cs

Określa, czy ten ciąg i określony String obiekt mają taką samą wartość. Parametr określa kulturę, wielkość liter i reguły sortowania używane w porównaniu.

public:
 bool Equals(System::String ^ value, StringComparison comparisonType);
public bool Equals (string value, StringComparison comparisonType);
public bool Equals (string? value, StringComparison comparisonType);
override this.Equals : string * StringComparison -> bool
Public Function Equals (value As String, comparisonType As StringComparison) As Boolean

Parametry

value
String

Ciąg do porównania z tym wystąpieniem.

comparisonType
StringComparison

Jedna z wartości wyliczenia określająca sposób porównywania ciągów.

Zwraca

truejeśli wartość parametru value jest taka sama jak ten ciąg; w przeciwnym razie . false

Wyjątki

comparisonType nie jest wartością StringComparison .

Przykłady

Poniższy przykład tworzy tablicę ciągów składającą się z wielkich liter "I", małych liter "i" i kropki "ı". Następnie wywołuje metodę Equals(String, StringComparison) , aby je porównać przy użyciu każdej możliwej StringComparison wartości wyliczenia.

using System;

class Sample 
{
   public static void Main() 
   {
      // Define a string array with the following three "I" characters:
      //      U+0069, U+0131, and U+0049.  
      string[] threeIs = { "i", "ı", "I" };
      // Define Type object representing StringComparison type.
      Type scType = typeof(StringComparison);  
      
      // Show the current culture (for culture-sensitive string comparisons).
      Console.WriteLine("The current culture is {0}.\n", 
                        System.Globalization.CultureInfo.CurrentCulture.Name);
        
      // Perform comparisons using each StringComparison member. 
      foreach (string scName in Enum.GetNames(scType))
      {
         StringComparison sc = (StringComparison) Enum.Parse(scType, scName);
         Console.WriteLine("Comparisons using {0}:", sc);
         // Compare each character in character array.
         for (int ctr = 0; ctr <= 1; ctr++)
         {
            string instanceChar = threeIs[ctr];
            for (int innerCtr = ctr + 1; innerCtr <= threeIs.GetUpperBound(0); innerCtr++)
            {
               string otherChar = threeIs[innerCtr];
               Console.WriteLine("{0} (U+{1}) = {2} (U+{3}): {4}", 
                                 instanceChar, Convert.ToInt16(Char.Parse(instanceChar)).ToString("X4"), 
                                 otherChar, Convert.ToInt16(Char.Parse(otherChar)).ToString("X4"), 
                                 instanceChar.Equals(otherChar, sc));
            }
            Console.WriteLine();
         }
      }   
   }
}
// The example displays the following output:
//       The current culture is en-US.
//       
//       Comparisons using CurrentCulture:
//       i (U+0069) = ı (U+0131): False
//       i (U+0069) = I (U+0049): False
//       
//       ı (U+0131) = I (U+0049): False
//       
//       Comparisons using CurrentCultureIgnoreCase:
//       i (U+0069) = ı (U+0131): False
//       i (U+0069) = I (U+0049): True
//       
//       ı (U+0131) = I (U+0049): False
//       
//       Comparisons using InvariantCulture:
//       i (U+0069) = ı (U+0131): False
//       i (U+0069) = I (U+0049): False
//       
//       ı (U+0131) = I (U+0049): False
//       
//       Comparisons using InvariantCultureIgnoreCase:
//       i (U+0069) = ı (U+0131): False
//       i (U+0069) = I (U+0049): True
//       
//       ı (U+0131) = I (U+0049): False
//       
//       Comparisons using Ordinal:
//       i (U+0069) = ı (U+0131): False
//       i (U+0069) = I (U+0049): False
//       
//       ı (U+0131) = I (U+0049): False
//       
//       Comparisons using OrdinalIgnoreCase:
//       i (U+0069) = ı (U+0131): False
//       i (U+0069) = I (U+0049): True
//       
//       ı (U+0131) = I (U+0049): False
open System
open System.Globalization

// Define a string array with the following three "I" characters:
//      U+0069, U+0131, and U+0049.  
let threeIs = [| "i"; "ı"; "I" |]
// Define Type object representing StringComparison type.
let scType = typeof<StringComparison>  

// Show the current culture (for culture-sensitive string comparisons).
printfn $"The current culture is {CultureInfo.CurrentCulture.Name}.\n"

// Perform comparisons using each StringComparison member. 
for scName in Enum.GetNames scType do
    let sc = Enum.Parse(scType, scName) :?> StringComparison
    printfn $"Comparisons using {sc}:"
    // Compare each character in character array.
    for ctr = 0 to 1 do
        let instanceChar = threeIs[ctr]
        for innerCtr = ctr + 1 to threeIs.GetUpperBound 0 do
            let otherChar = threeIs[innerCtr]
            printfn $"{instanceChar} (U+{Convert.ToInt16(Char.Parse instanceChar):X4}) = {otherChar} (U+{Convert.ToInt16(Char.Parse otherChar):X4}): {instanceChar.Equals(otherChar, sc)}"
        printfn ""
// The example displays the following output:
//       The current culture is en-US.
//       
//       Comparisons using CurrentCulture:
//       i (U+0069) = i (U+0131): False
//       i (U+0069) = I (U+0049): False
//       
//       i (U+0131) = I (U+0049): False
//       
//       Comparisons using CurrentCultureIgnoreCase:
//       i (U+0069) = i (U+0131): False
//       i (U+0069) = I (U+0049): True
//       
//       i (U+0131) = I (U+0049): False
//       
//       Comparisons using InvariantCulture:
//       i (U+0069) = i (U+0131): False
//       i (U+0069) = I (U+0049): False
//       
//       i (U+0131) = I (U+0049): False
//       
//       Comparisons using InvariantCultureIgnoreCase:
//       i (U+0069) = i (U+0131): False
//       i (U+0069) = I (U+0049): True
//       
//       i (U+0131) = I (U+0049): False
//       
//       Comparisons using Ordinal:
//       i (U+0069) = i (U+0131): False
//       i (U+0069) = I (U+0049): False
//       
//       i (U+0131) = I (U+0049): False
//       
//       Comparisons using OrdinalIgnoreCase:
//       i (U+0069) = i (U+0131): False
//       i (U+0069) = I (U+0049): True
//       
//       i (U+0131) = I (U+0049): False
Class Sample
   Public Shared Sub Main() 
      ' Define a string array with the following three "I" characters:
      '      U+0069, U+0131, and U+0049.  
      Dim threeIs() As String = { "i", "ı", "I" }
      ' Define Type object representing StringComparison type.
      Dim scType As Type = GetType(StringComparison)  
      
      ' Show the current culture (for culture-sensitive string comparisons).
      Console.WriteLine("The current culture is {0}." & vbCrLf, _
                        System.Globalization.CultureInfo.CurrentCulture.Name)
        
      ' Perform comparisons using each StringComparison member. 
      For Each scName As String In [Enum].GetNames(scType)
         Dim sc As StringComparison = [Enum].Parse(scType, scName)
         Console.WriteLine("Comparisons using {0}:", sc)
         ' Compare each character in character array.
         For ctr As Integer = 0 To 1
            Dim instanceChar As String = threeIs(ctr)
            For innerCtr As Integer = ctr + 1 To threeIs.GetUpperBound(0)
               Dim otherChar As STring = threeIs(innerCtr)
               Console.WriteLine("{0} (U+{1}) = {2} (U+{3}): {4}", _
                                 instanceChar, Convert.ToInt16(Char.Parse(instanceChar)).ToString("X4"), _
                                 otherChar, Convert.ToInt16(Char.Parse(otherChar)).ToString("X4"), _
                                 instanceChar.Equals(otherChar, sc))
            Next
            Console.WriteLine()
         Next
      Next              
   End Sub
End Class
' The example displays the following output:
'       The current culture is en-US.
'       
'       Comparisons using CurrentCulture:
'       i (U+0069) = i (U+0131): False
'       i (U+0069) = I (U+0049): False
'       
'       i (U+0131) = I (U+0049): False
'       
'       Comparisons using CurrentCultureIgnoreCase:
'       i (U+0069) = i (U+0131): False
'       i (U+0069) = I (U+0049): True
'       
'       i (U+0131) = I (U+0049): False
'       
'       Comparisons using InvariantCulture:
'       i (U+0069) = i (U+0131): False
'       i (U+0069) = I (U+0049): False
'       
'       i (U+0131) = I (U+0049): False
'       
'       Comparisons using InvariantCultureIgnoreCase:
'       i (U+0069) = i (U+0131): False
'       i (U+0069) = I (U+0049): True
'       
'       i (U+0131) = I (U+0049): False
'       
'       Comparisons using Ordinal:
'       i (U+0069) = i (U+0131): False
'       i (U+0069) = I (U+0049): False
'       
'       i (U+0131) = I (U+0049): False
'       
'       Comparisons using OrdinalIgnoreCase:
'       i (U+0069) = i (U+0131): False
'       i (U+0069) = I (U+0049): True
'       
'       i (U+0131) = I (U+0049): False

Uwagi

Parametr comparisonType wskazuje, czy porównanie powinno używać bieżącej lub niezmiennej kultury, honorować lub ignorować przypadek dwóch ciągów porównywanych, lub używać reguł sortowania wyrazów lub porządkowych.

Zobacz też

Dotyczy

Equals(String, String, StringComparison)

Źródło:
String.Comparison.cs
Źródło:
String.Comparison.cs
Źródło:
String.Comparison.cs

Określa, czy dwa określone String obiekty mają tę samą wartość. Parametr określa kulturę, wielkość liter i reguły sortowania używane w porównaniu.

public:
 static bool Equals(System::String ^ a, System::String ^ b, StringComparison comparisonType);
public static bool Equals (string a, string b, StringComparison comparisonType);
public static bool Equals (string? a, string? b, StringComparison comparisonType);
static member Equals : string * string * StringComparison -> bool
Public Shared Function Equals (a As String, b As String, comparisonType As StringComparison) As Boolean

Parametry

a
String

Pierwszy ciąg do porównania lub null.

b
String

Drugi ciąg do porównania lub null.

comparisonType
StringComparison

Jedna z wartości wyliczenia, która określa reguły porównania.

Zwraca

true jeśli wartość parametru a jest równa wartości parametru b ; w przeciwnym razie wartość false.

Wyjątki

comparisonType nie jest wartością StringComparison .

Przykłady

Poniższy przykład porównuje trzy zestawy ciągów przy użyciu każdego elementu członkowskiego StringComparison wyliczenia. Porównania używają konwencji języka angielskiego (Stany Zjednoczone), tajskich (Tajlandia) i tureckich (Turcja). Należy pamiętać, że ciągi "a" i "a-" są uważane za równoważne w kulturze "th-TH", ale nie w innych, podczas gdy "i" i "İ" są uważane za równoważne w kulturze "tr-TR", gdy przypadek jest ignorowany, ale nie w innych kulturach.

using System;
using System.Globalization;
using System.Threading;

public class Example3
{
    public static void Main()
    {
        String[] cultureNames = { "en-US", "th-TH", "tr-TR" };
        String[] strings1 = { "a", "i", "case", };
        String[] strings2 = { "a-", "\u0130", "Case" };
        StringComparison[] comparisons = (StringComparison[])Enum.GetValues(typeof(StringComparison));

        foreach (var cultureName in cultureNames)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
            Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.Name);
            for (int ctr = 0; ctr <= strings1.GetUpperBound(0); ctr++)
            {
                foreach (var comparison in comparisons)
                    Console.WriteLine("   {0} = {1} ({2}): {3}", strings1[ctr],
                                      strings2[ctr], comparison,
                                      String.Equals(strings1[ctr], strings2[ctr], comparison));

                Console.WriteLine();
            }
            Console.WriteLine();
        }
    }
}
// The example displays the following output:
//    Current Culture: en-US
//       a = a- (CurrentCulture): False
//       a = a- (CurrentCultureIgnoreCase): False
//       a = a- (InvariantCulture): False
//       a = a- (InvariantCultureIgnoreCase): False
//       a = a- (Ordinal): False
//       a = a- (OrdinalIgnoreCase): False
//
//       i = İ (CurrentCulture): False
//       i = İ (CurrentCultureIgnoreCase): False
//       i = İ (InvariantCulture): False
//       i = İ (InvariantCultureIgnoreCase): False
//       i = İ (Ordinal): False
//       i = İ (OrdinalIgnoreCase): False
//
//       case = Case (CurrentCulture): False
//       case = Case (CurrentCultureIgnoreCase): True
//       case = Case (InvariantCulture): False
//       case = Case (InvariantCultureIgnoreCase): True
//       case = Case (Ordinal): False
//       case = Case (OrdinalIgnoreCase): True
//
//
//    Current Culture: th-TH
//       a = a- (CurrentCulture): True
//       a = a- (CurrentCultureIgnoreCase): True
//       a = a- (InvariantCulture): False
//       a = a- (InvariantCultureIgnoreCase): False
//       a = a- (Ordinal): False
//       a = a- (OrdinalIgnoreCase): False
//
//       i = İ (CurrentCulture): False
//       i = İ (CurrentCultureIgnoreCase): False
//       i = İ (InvariantCulture): False
//       i = İ (InvariantCultureIgnoreCase): False
//       i = İ (Ordinal): False
//       i = İ (OrdinalIgnoreCase): False
//
//       case = Case (CurrentCulture): False
//       case = Case (CurrentCultureIgnoreCase): True
//       case = Case (InvariantCulture): False
//       case = Case (InvariantCultureIgnoreCase): True
//       case = Case (Ordinal): False
//       case = Case (OrdinalIgnoreCase): True
//
//
//    Current Culture: tr-TR
//       a = a- (CurrentCulture): False
//       a = a- (CurrentCultureIgnoreCase): False
//       a = a- (InvariantCulture): False
//       a = a- (InvariantCultureIgnoreCase): False
//       a = a- (Ordinal): False
//       a = a- (OrdinalIgnoreCase): False
//
//       i = İ (CurrentCulture): False
//       i = İ (CurrentCultureIgnoreCase): True
//       i = İ (InvariantCulture): False
//       i = İ (InvariantCultureIgnoreCase): False
//       i = İ (Ordinal): False
//       i = İ (OrdinalIgnoreCase): False
//
//       case = Case (CurrentCulture): False
//       case = Case (CurrentCultureIgnoreCase): True
//       case = Case (InvariantCulture): False
//       case = Case (InvariantCultureIgnoreCase): True
//       case = Case (Ordinal): False
//       case = Case (OrdinalIgnoreCase): True
open System
open System.Globalization
open System.Threading

let cultureNames = 
    [| "en-US"; "se-SE" |]
let strings1 = 
    [| "case"; "encyclopædia" 
       "encyclopædia"; "Archæology" |]
let strings2 = 
    [| "Case"; "encyclopaedia" 
       "encyclopedia"; "ARCHÆOLOGY" |]
let comparisons = 
    Enum.GetValues typeof<StringComparison> :?> StringComparison[]

for cultureName in cultureNames do
    Thread.CurrentThread.CurrentCulture <- CultureInfo.CreateSpecificCulture cultureName
    printfn $"Current Culture: {CultureInfo.CurrentCulture.Name}"
    for i = 0 to strings1.GetUpperBound 0 do
        for comparison in comparisons do
            printfn $"   {strings1[i]} = {strings2[i]} ({comparison}): {String.Equals(strings1[i], strings2[i], comparison)}"
        printfn ""         
    printfn ""
// The example displays the following output:
//    Current Culture: en-US
//       case = Case (CurrentCulture): False
//       case = Case (CurrentCultureIgnoreCase): True
//       case = Case (InvariantCulture): False
//       case = Case (InvariantCultureIgnoreCase): True
//       case = Case (Ordinal): False
//       case = Case (OrdinalIgnoreCase): True
//    
//       encyclopædia = encyclopaedia (CurrentCulture): True
//       encyclopædia = encyclopaedia (CurrentCultureIgnoreCase): True
//       encyclopædia = encyclopaedia (InvariantCulture): True
//       encyclopædia = encyclopaedia (InvariantCultureIgnoreCase): True
//       encyclopædia = encyclopaedia (Ordinal): False
//       encyclopædia = encyclopaedia (OrdinalIgnoreCase): False
//    
//       encyclopædia = encyclopedia (CurrentCulture): False
//       encyclopædia = encyclopedia (CurrentCultureIgnoreCase): False
//       encyclopædia = encyclopedia (InvariantCulture): False
//       encyclopædia = encyclopedia (InvariantCultureIgnoreCase): False
//       encyclopædia = encyclopedia (Ordinal): False
//       encyclopædia = encyclopedia (OrdinalIgnoreCase): False
//    
//       Archæology = ARCHÆOLOGY (CurrentCulture): False
//       Archæology = ARCHÆOLOGY (CurrentCultureIgnoreCase): True
//       Archæology = ARCHÆOLOGY (InvariantCulture): False
//       Archæology = ARCHÆOLOGY (InvariantCultureIgnoreCase): True
//       Archæology = ARCHÆOLOGY (Ordinal): False
//       Archæology = ARCHÆOLOGY (OrdinalIgnoreCase): True
//    
//    
//    Current Culture: se-SE
//       case = Case (CurrentCulture): False
//       case = Case (CurrentCultureIgnoreCase): True
//       case = Case (InvariantCulture): False
//       case = Case (InvariantCultureIgnoreCase): True
//       case = Case (Ordinal): False
//       case = Case (OrdinalIgnoreCase): True
//    
//       encyclopædia = encyclopaedia (CurrentCulture): False
//       encyclopædia = encyclopaedia (CurrentCultureIgnoreCase): False
//       encyclopædia = encyclopaedia (InvariantCulture): True
//       encyclopædia = encyclopaedia (InvariantCultureIgnoreCase): True
//       encyclopædia = encyclopaedia (Ordinal): False
//       encyclopædia = encyclopaedia (OrdinalIgnoreCase): False
//    
//       encyclopædia = encyclopedia (CurrentCulture): False
//       encyclopædia = encyclopedia (CurrentCultureIgnoreCase): False
//       encyclopædia = encyclopedia (InvariantCulture): False
//       encyclopædia = encyclopedia (InvariantCultureIgnoreCase): False
//       encyclopædia = encyclopedia (Ordinal): False
//       encyclopædia = encyclopedia (OrdinalIgnoreCase): False
//    
//       Archæology = ARCHÆOLOGY (CurrentCulture): False
//       Archæology = ARCHÆOLOGY (CurrentCultureIgnoreCase): True
//       Archæology = ARCHÆOLOGY (InvariantCulture): False
//       Archæology = ARCHÆOLOGY (InvariantCultureIgnoreCase): True
//       Archæology = ARCHÆOLOGY (Ordinal): False
//       Archæology = ARCHÆOLOGY (OrdinalIgnoreCase): True
Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "se-SE" }
      Dim strings1() As String = { "case",  "encyclopædia",  
                                   "encyclopædia", "Archæology" }
      Dim strings2() As String = { "Case", "encyclopaedia", 
                                   "encyclopedia" , "ARCHÆOLOGY" }
      Dim comparisons() As StringComparison = CType([Enum].GetValues(GetType(StringComparison)),
                                           StringComparison())
      
      For Each cultureName In cultureNames
         Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName)
         Console.WriteLine("Current Culture: {0}", CultureInfo.CurrentCulture.Name)
         For ctr As Integer = 0 To strings1.GetUpperBound(0)
            For Each comparison In comparisons
               Console.WriteLine("   {0} = {1} ({2}): {3}", strings1(ctr),
                                 strings2(ctr), comparison, 
                                 String.Equals(strings1(ctr), strings2(ctr), comparison))
            Next
            Console.WriteLine()         
         Next
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays the following output:
'    Current Culture: en-US
'       case = Case (CurrentCulture): False
'       case = Case (CurrentCultureIgnoreCase): True
'       case = Case (InvariantCulture): False
'       case = Case (InvariantCultureIgnoreCase): True
'       case = Case (Ordinal): False
'       case = Case (OrdinalIgnoreCase): True
'    
'       encyclopædia = encyclopaedia (CurrentCulture): True
'       encyclopædia = encyclopaedia (CurrentCultureIgnoreCase): True
'       encyclopædia = encyclopaedia (InvariantCulture): True
'       encyclopædia = encyclopaedia (InvariantCultureIgnoreCase): True
'       encyclopædia = encyclopaedia (Ordinal): False
'       encyclopædia = encyclopaedia (OrdinalIgnoreCase): False
'    
'       encyclopædia = encyclopedia (CurrentCulture): False
'       encyclopædia = encyclopedia (CurrentCultureIgnoreCase): False
'       encyclopædia = encyclopedia (InvariantCulture): False
'       encyclopædia = encyclopedia (InvariantCultureIgnoreCase): False
'       encyclopædia = encyclopedia (Ordinal): False
'       encyclopædia = encyclopedia (OrdinalIgnoreCase): False
'    
'       Archæology = ARCHÆOLOGY (CurrentCulture): False
'       Archæology = ARCHÆOLOGY (CurrentCultureIgnoreCase): True
'       Archæology = ARCHÆOLOGY (InvariantCulture): False
'       Archæology = ARCHÆOLOGY (InvariantCultureIgnoreCase): True
'       Archæology = ARCHÆOLOGY (Ordinal): False
'       Archæology = ARCHÆOLOGY (OrdinalIgnoreCase): True
'    
'    
'    Current Culture: se-SE
'       case = Case (CurrentCulture): False
'       case = Case (CurrentCultureIgnoreCase): True
'       case = Case (InvariantCulture): False
'       case = Case (InvariantCultureIgnoreCase): True
'       case = Case (Ordinal): False
'       case = Case (OrdinalIgnoreCase): True
'    
'       encyclopædia = encyclopaedia (CurrentCulture): False
'       encyclopædia = encyclopaedia (CurrentCultureIgnoreCase): False
'       encyclopædia = encyclopaedia (InvariantCulture): True
'       encyclopædia = encyclopaedia (InvariantCultureIgnoreCase): True
'       encyclopædia = encyclopaedia (Ordinal): False
'       encyclopædia = encyclopaedia (OrdinalIgnoreCase): False
'    
'       encyclopædia = encyclopedia (CurrentCulture): False
'       encyclopædia = encyclopedia (CurrentCultureIgnoreCase): False
'       encyclopædia = encyclopedia (InvariantCulture): False
'       encyclopædia = encyclopedia (InvariantCultureIgnoreCase): False
'       encyclopædia = encyclopedia (Ordinal): False
'       encyclopædia = encyclopedia (OrdinalIgnoreCase): False
'    
'       Archæology = ARCHÆOLOGY (CurrentCulture): False
'       Archæology = ARCHÆOLOGY (CurrentCultureIgnoreCase): True
'       Archæology = ARCHÆOLOGY (InvariantCulture): False
'       Archæology = ARCHÆOLOGY (InvariantCultureIgnoreCase): True
'       Archæology = ARCHÆOLOGY (Ordinal): False
'       Archæology = ARCHÆOLOGY (OrdinalIgnoreCase): True

Uwagi

Parametr comparisonType wskazuje, czy porównanie powinno używać bieżącej lub niezmiennej kultury, honorować lub ignorować przypadek dwóch ciągów porównywanych, lub używać reguł sortowania wyrazów lub porządkowych.

Zobacz też

Dotyczy