How to compare strings with case insensitive and accent insensitive

后端 未结 2 1131
-上瘾入骨i
-上瘾入骨i 2021-01-04 21:46

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

相关标签:
2条回答
  • 2021-01-04 22:18

    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.

    0 讨论(0)
  • 2021-01-04 22:36

    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));
    
    0 讨论(0)
提交回复
热议问题