Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of when the culture parameter of string.Equals (c#) actually makes a difference?

Tags:

string

c#

I don’t fully understand the second parameter of string.Equals, and this is because I can’t find any examples of when it would actually make a difference. For example, the example given here is the same, regardless of the value of the second parameter (aside from IgnoreCase): http://msdn.microsoft.com/en-us/library/c64xh8f9.aspx

I am just talking about the values StringComparison.CurrentCulture, InvariantCulture, or Ordinal.
I can understand the difference between these and their IgnoreCase equivalents.

like image 381
zod Avatar asked May 27 '10 08:05

zod


People also ask

How do you match a string in C#?

The C# Compare() method is used to compare first string with second string lexicographically. It returns an integer value. If both strings are equal, it returns 0. If first string is greater than second string, it returns 1 else it returns -1.

What is StringComparison ordinal C#?

The StringComparison enumeration is used to specify whether a string comparison should use the current culture or the invariant culture, word or ordinal sort rules, and be case-sensitive or case-insensitive. Important. When you call a string comparison method such as String. Compare, String.

What is the difference between equals () and == in C#?

Difference between == and . Equals method in c# The Equality Operator ( ==) is the comparison operator and the Equals() method in C# is used to compare the content of a string. The Equals() method compares only content.


2 Answers

This MSDN page (Best Practices for Using Strings in the .NET Framework) has a lot of information about using strings and the following example is taken from it:

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

public class Example
{
   public static void Main()
   {
      string[] values= { "able", "ångström", "apple", "Æble", 
                         "Windows", "Visual Studio" };
      Array.Sort(values);
      DisplayArray(values);

      // Change culture to Swedish (Sweden).
      string originalCulture = CultureInfo.CurrentCulture.Name;
      Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE");
      Array.Sort(values);
      DisplayArray(values);

      // Restore the original culture.
      Thread.CurrentThread.CurrentCulture = new CultureInfo(originalCulture);
    }

    private static void DisplayArray(string[] values)
    {
      Console.WriteLine("Sorting using the {0} culture:",  
                        CultureInfo.CurrentCulture.Name);
      foreach (string value in values)
         Console.WriteLine("   {0}", value);

      Console.WriteLine();
    }
}
// The example displays the following output:
//       Sorting using the en-US culture:
//          able
//          Æble
//          ångström
//          apple
//          Visual Studio
//          Windows
//       
//       Sorting using the sv-SE culture:
//          able
//          Æble
//          apple
//          Windows
//          Visual Studio
//          ångström
like image 56
Oded Avatar answered Nov 10 '22 08:11

Oded


Differences between StringComparison.InvariantCulture and StringComparison.Ordinal are fairly easy to find, since Ordinal means that the string is not normalized before it is compared. So we just have to compare a normalized string to an unnormalized string.

Finding differences between StringComparison.InvariantCulture and StringComparison.CurrentCulture (or differences between different CurrentCultures) is a bit more difficult, but they do exist.

Here is one example:

    string a = "\u00C4";       // "LATIN CAPITAL LETTER A WITH DIAERESIS"
    string b = "\u0041\u0308"; // "LATIN CAPITAL LETTER A" - "COMBINING DIAERESIS"

    Console.WriteLine(a.Equals(b, StringComparison.InvariantCulture)); // true

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
    Console.WriteLine(a.Equals(b, StringComparison.CurrentCulture));   // true

    Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK", false);
    Console.WriteLine(a.Equals(b, StringComparison.CurrentCulture));   // false

    Console.WriteLine(a.Equals(b, StringComparison.Ordinal));          // false

Or this one that only uses ASCII characters:

    string ddzs = "ddzs";
    string dzsdzs = "dzsdzs";

    Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US", false);
    Console.WriteLine(ddzs.Equals(dzsdzs, StringComparison.CurrentCulture)); // false

    Thread.CurrentThread.CurrentCulture = new CultureInfo("hu-HU", false);
    Console.WriteLine(ddzs.Equals(dzsdzs, StringComparison.CurrentCulture)); // true
like image 42
Rasmus Faber Avatar answered Nov 10 '22 08:11

Rasmus Faber