System.String.Equals 方法 (String, StringComparison)

方法描述

确定此字符串是否与指定的 String 对象具有相同的值。 参数指定区域性、大小写以及比较所用的排序规则。

语法定义(C# System.String.Equals 方法 (String, StringComparison) 的用法)

public bool Equals(
	string value,
	StringComparison comparisonType
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
value System-String 要与此实例进行比较的字符串。
comparisonType System-StringComparison 枚举值之一,用于指定将如何比较字符串。
返回值 System.Boolean 如果 value 参数的值与此字符串相同,则为 true;否则为 false。

提示和注释

comparisonType 参数指示比较应使用当前区域性还是固定区域性,应考虑还是忽略两个待比较字符串的大小写,以及应使用字排序规则还是序号排序规则。

System.String.Equals 方法 (String, StringComparison)例子

然后,它调用 Equals(String, StringComparison) 方法,通过使用每个可能的 StringComparison 枚举值进行比较。

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) = 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

异常

异常 异常描述
ArgumentException comparisonType 不是一个 StringComparison 值。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。