How to compare strings with case insensitive and accent insensitive

后端 未结 2 1132
-上瘾入骨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.

提交回复
热议问题