Example of when the culture parameter of string.Equals (c#) actually makes a difference?
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 differe开发者_StackOverflownce. 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.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
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 CurrentCulture
s) 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
精彩评论