英語で読む

次の方法で共有


String.Equals メソッド

定義

2 つの String オブジェクトの値が同一かどうかを判断します。

オーバーロード

Equals(Object)

このインスタンスと、指定したオブジェクトの値が同一かどうかを判断します。String オブジェクトを指定する必要があります。

Equals(String)

このインスタンスと、指定した別の String の値が同一かどうかを判断します。

Equals(String, String)

指定した 2 つの String オブジェクトの値が同一かどうかを判断します。

Equals(String, StringComparison)

この文字列と、指定した String オブジェクトの値が同一かどうかを判断します。 比較に使用するカルチャ、大文字と小文字の区別、および、並べ替え規則をパラメーターで指定します。

Equals(String, String, StringComparison)

指定した 2 つの String オブジェクトの値が同一かどうかを判断します。 比較に使用するカルチャ、大文字と小文字の区別、および、並べ替え規則をパラメーターで指定します。

Equals(Object)

ソース:
String.Comparison.cs
ソース:
String.Comparison.cs
ソース:
String.Comparison.cs

このインスタンスと、指定したオブジェクトの値が同一かどうかを判断します。String オブジェクトを指定する必要があります。

C#
public override bool Equals (object obj);
C#
public override bool Equals (object? obj);

パラメーター

obj
Object

このインスタンスと比較する文字列。

戻り値

trueobj で、このインスタンスと同じ値を保持している場合は String。それ以外の場合は falseobjnull の場合、メソッドは false を返します。

Equalsメソッドの例を次に示します。

C#
// 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
*/

注釈

このメソッドは、序数 (大文字と小文字を区別し、カルチャに依存しない) 比較を実行します。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Equals(String)

ソース:
String.Comparison.cs
ソース:
String.Comparison.cs
ソース:
String.Comparison.cs

このインスタンスと、指定した別の String の値が同一かどうかを判断します。

C#
public bool Equals (string value);
C#
public bool Equals (string? value);

パラメーター

value
String

このインスタンスと比較する文字列。

戻り値

true パラメーターの値がこのインスタンスの値と同じ場合は value。それ以外の場合は falsevaluenull の場合、メソッドは false を返します。

実装

Equalsメソッドの例を次に示します。 タイトルの大文字と小文字の単語 "File" と、対応する単語、小文字に相当する単語、大文字に相当する単語、ラテン小文字 I (U+0069) ではなくラテン小文字の DOTLESS I (U+0131) を含む単語を比較します。 メソッドは Equals(String) 序数比較を実行するため、同じ単語との比較のみが を返します true

C#
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

注釈

このメソッドは、序数 (大文字と小文字を区別し、カルチャに依存しない) 比較を実行します。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Equals(String, String)

ソース:
String.Comparison.cs
ソース:
String.Comparison.cs
ソース:
String.Comparison.cs

指定した 2 つの String オブジェクトの値が同一かどうかを判断します。

C#
public static bool Equals (string a, string b);
C#
public static bool Equals (string? a, string? b);

パラメーター

a
String

比較する最初の文字列または null

b
String

比較する 2 番目の文字列または null

戻り値

a の値が b の値と同じ場合は true。それ以外の場合は falseab の両方が null の場合、メソッドは true を返します。

Equalsメソッドの例を次に示します。

C#
// 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
*/

注釈

このメソッドは、序数 (大文字と小文字を区別し、カルチャに依存しない) 比較を実行します。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Equals(String, StringComparison)

ソース:
String.Comparison.cs
ソース:
String.Comparison.cs
ソース:
String.Comparison.cs

この文字列と、指定した String オブジェクトの値が同一かどうかを判断します。 比較に使用するカルチャ、大文字と小文字の区別、および、並べ替え規則をパラメーターで指定します。

C#
public bool Equals (string value, StringComparison comparisonType);
C#
public bool Equals (string? value, StringComparison comparisonType);

パラメーター

value
String

このインスタンスと比較する文字列。

comparisonType
StringComparison

文字列の比較方法を指定する列挙値の 1 つ。

戻り値

true パラメーターの値がこの文字列と同じ場合は value。それ以外の場合は false

例外

comparisonTypeStringComparison 値ではありません。

次の例では、大文字の "I"、小文字の "i"、ドットなしの "ı" で構成される文字列配列を作成します。 次に、 メソッドを Equals(String, StringComparison) 呼び出して、可能な StringComparison 列挙値を使用して比較します。

C#
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

注釈

パラメーターは comparisonType 、比較で現在のカルチャまたはインバリアント カルチャを使用するか、比較対象の 2 つの文字列の大文字と小文字を区別するか無視するか、単語または序数の並べ替えルールを使用するかを示します。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Equals(String, String, StringComparison)

ソース:
String.Comparison.cs
ソース:
String.Comparison.cs
ソース:
String.Comparison.cs

指定した 2 つの String オブジェクトの値が同一かどうかを判断します。 比較に使用するカルチャ、大文字と小文字の区別、および、並べ替え規則をパラメーターで指定します。

C#
public static bool Equals (string a, string b, StringComparison comparisonType);
C#
public static bool Equals (string? a, string? b, StringComparison comparisonType);

パラメーター

a
String

比較する最初の文字列または null

b
String

比較する 2 番目の文字列または null

comparisonType
StringComparison

比較の規則を指定する列挙値の 1 つ。

戻り値

true パラメーターの値が a パラメーターの値に等しい場合は b。それ以外の場合は false

例外

comparisonTypeStringComparison 値ではありません。

次の例では、 列挙体の各メンバーを使用して、3 つの文字列セットを StringComparison 比較します。 比較では、英語 (米国)、タイ語 (タイ)、トルコ語 (トルコ) の文化の規則が使用されます。 文字列 "a" と "a-" は "th-TH" カルチャでは同等と見なされますが、他のカルチャでは同じと見なされますが、"i" と "İ" は大文字と小文字が無視されますが、他のカルチャでは "tr-TR" カルチャでは同等と見なされることに注意してください。

C#
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

注釈

パラメーターは comparisonType 、比較で現在のカルチャまたはインバリアント カルチャを使用するか、比較対象の 2 つの文字列の大文字と小文字を区別するか無視するか、単語または序数の並べ替えルールを使用するかを示します。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0