连字符后的字符影响string.compare [英] Character after hyphen affects string.compare

查看:49
本文介绍了连字符后的字符影响string.compare的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望"2-"和"22"总是以相同的方式进行比较,但是更改第3个字符会改变排序顺序.

I would expect "2-" and "22" to always compare the same way, but changing the 3rd character changes the sort order.

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

这到底是怎么回事?

顺便说一下,我们的文化是美国文化.

Our culture is en-US by the way.

推荐答案

按照

字符集包含可忽略的字符.当执行区分文化的比较时, Compare(String,String,StringComparison)方法不考虑此类字符.要在比较中识别可忽略的字符,请为 comparisonType 参数提供 StringComparison.Ordinal OrdinalIgnoreCase 值.

Character sets include ignorable characters. The Compare(String, String, StringComparison) method does not consider such characters when it performs a culture-sensitive comparison. To recognize ignorable characters in your comparison, supply a value of StringComparison.Ordinal or OrdinalIgnoreCase for the comparisonType parameter.

在您的情况下,连字符是可忽略的字符.在这种情况下,这意味着不考虑中间的连字符.这意味着它实际上将 21 22 22-进行比较,这将得出 -1 1 .

In your case, the hyphen is an ignorable character. In this case it means that the middle hyphen isn't taken into account. That means it actually compares 21 and 22 to 22-, which will evaluate to -1 and 1 respectively.

这篇关于连字符后的字符影响string.compare的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆