A hyphen character affects string.compare

I would expect that “2-” and “22” would always compare the same, but changing the third character changes the sort order.

string.Compare("2-1","22-", StringComparison.CurrentCulture) //-1
string.Compare("2-2","22-", StringComparison.CurrentCulture) //1

What is going on here?

Our culture, by the way, is in the USA.

+4
source share
1 answer

According to the documentation :

Character sets include unknowing characters. The method Compare(String, String, StringComparison)does not take into account such characters when it performs a culturally sensitive comparison. To recognize ignorant characters in your comparison, set the value to StringComparison.Ordinalor OrdinalIgnoreCasefor the parameter comparisonType.

. , . , 21 22 22-, -1 1.

+7

Source: https://habr.com/ru/post/1679806/


All Articles