问题
How to compare strings with case insensitive and accent insensitive
Alright this is done easily at SQL server
However I would like to do the same at C# .NET 4.5.1.
How can I do that with most proper way?
I mean these 3 strings should return equal when compared
http://www.buroteknik.com/metylan-c387c4b0ft-tarafli-bant-12cm-x25mt_154202.html
http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLI-BANT-12cm-x25mt_154202.html
http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLı-BANT-12cm-x25mt_154202.html
I need a method that would say these 2 below are same SQL server says they are equal.
tarafli
TARAFLİ
回答1:
To ignore both case AND accents, you can use string.Compare()
with both the IgnoreNonSpace
AND the IgnoreCase
options, like so:
string s1 = "http://www.buroteknik.com/metylan-c387c4b0ft-tarafli-bant-12cm-x25mt_154202.html";
string s2 = "http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLI-BANT-12cm-x25mt_154202.html";
string s3 = "http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLı-BANT-12cm-x25mt_154202.html";
Console.WriteLine(string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
Console.WriteLine(string.Compare(s2, s3, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
In response to your comments below, this works for tarafli
and TARAFLİ
too.
The following code prints 0, meaning the strings are equal:
string s1 = "tarafli";
string s2 = "TARAFLİ";
Console.WriteLine(string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
And here it is using the Turkish culture (I'm guessing at what the correct culture is). This also prints 0:
string s1 = "tarafli";
string s2 = "TARAFLİ";
var trlocale = CultureInfo.GetCultureInfo("tr-TR");
Console.WriteLine(string.Compare(s1, s2, trlocale, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
回答2:
You can use string.Compare
with the overload which takes the proper CultureInfo
and CompareOptions
:
string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace |
CompareOptions.IgnoreCase);
Edit:
As for your question on CultureInfo
, from MSDN:
The comparison uses the culture parameter to obtain culture-specific information, such as casing rules and the alphabetical order of individual characters. For example, a particular culture could specify that certain combinations of characters be treated as a single character, that uppercase and lowercase characters be compared in a particular way, or that the sort order of a character depends on the characters that precede or follow it.
来源:https://stackoverflow.com/questions/27903503/how-to-compare-strings-with-case-insensitive-and-accent-insensitive