An example of when the culture parameter string.Equals (C #) really matters?

I do not fully understand the second parameter string.Equals, and this is because I cannot find examples of when this will really make a difference. For example, the example here is the same, regardless of the value of the second parameter (except for IgnoreCase): http://msdn.microsoft.com/en-us/library/c64xh8f9.aspx

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

+7
string c #
source share
2 answers

This MSDN page ("Guidelines for Using Strings in the .NET Framework") contains 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 
+4
source share

The differences between StringComparison.InvariantCulture and StringComparison.Ordinal fairly easy to find, because Ordinal means the string is not normalized before it is compared. Therefore, we just need to compare the normalized string with the non-normalized string.

Finding the differences between StringComparison.InvariantCulture and StringComparison.CurrentCulture (or the differences between different CurrentCulture s) is a bit more complicated, but they 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 one that uses only 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 
+3
source share

All Articles