C# String Equals(String, String, StringComparison)

Description

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

Syntax

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


public static bool Equals(
  string a,//from  w  ww.  j  a v a  2  s.  c o  m
  string b,
  StringComparison comparisonType
)

Parameters

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

  • a - The first string to compare, or null.
  • b - The second string to compare, or null.
  • comparisonType - One of the enumeration values that specifies the rules for the comparison.

Returns

String.Equals(String, String, StringComparison) method returns true if the value of the a parameter is equal to the value of the b parameter; otherwise, false.

Example


//ww  w  .  j  a  v a  2s . c o  m
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Main()
   {
      String[] cultureNames = { "en-US", "se-SE" };
      String[] strings1 = { "case",  "encyclop?dia",  
                            "encyclop?dia", "Arch?ology" };
      String[] strings2 = { "Case", "encyclopaedia", 
                            "encyclopedia" , "ARCH?OLOGY" };
      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 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