C# String Equals(String, StringComparison)

Description

String Equals(String, StringComparison) determines whether this string and a specified String object have the same value. A parameter specifies the culture, case, and sort rules used in the comparison.

Syntax

String.Equals(String, StringComparison) has the following syntax.


public bool Equals(
  string value,
  StringComparison comparisonType
)

Parameters

String.Equals(String, StringComparison) has the following parameters.

  • value - The string to compare to this instance.
  • comparisonType - One of the enumeration values that specifies how the strings will be compared.

Returns

String.Equals(String, StringComparison) method returns true if the value of the value parameter is the same as this string; otherwise, false.

Example

The following example creates a string array that consists of an uppercase "I", a lowercase "i", and a dotless "1".


/*from  ww  w . j a v  a 2s .  c om*/
using System;

class Sample 
{
   public static void Main() 
   {
      string[] letters = { "i", "I" };
      Type scType = typeof(StringComparison);  

      Console.WriteLine(System.Globalization.CultureInfo.CurrentCulture.Name);

      foreach (string scName in Enum.GetNames(scType))
      {
         StringComparison sc = (StringComparison) Enum.Parse(scType, scName);
         Console.WriteLine("Comparisons using {0}:", sc);
         for (int ctr = 0; ctr <= 1; ctr++)
         {
            string instanceChar = letters[ctr];
            for (int innerCtr = ctr + 1; innerCtr <= letters.GetUpperBound(0); innerCtr++)
            {
               string otherChar = letters[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 code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version