在.net字符串比较:" + QUOT; VS" - " [英] String comparison in .Net: "+" vs "-"

查看:257
本文介绍了在.net字符串比较:" + QUOT; VS" - "的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直认为的.Net比较字符串字典序,根据当前文化。但有一点很奇怪,当其中一个字符串结束对' - ':

I always assumed that .Net compares strings lexicographically, according to the current culture. But there is something strange when one of the strings ends on '-':

"+".CompareTo("-")
Returns: 1

"+1".CompareTo("-1")
Returns: -1

我得到它的所有的文化我试过了,其中包括不变量之一。 谁能解释是怎么回事,我怎么能得到一致的字符逐个字符排序当前区域?

I get it an all cultures I tried, including the invariant one. Can anyone explain what is going on, and how can I get the consistent character-by-character ordering for the current locale?

推荐答案

还有不一定是一致的字符由字符的排序为任何特定的语言环境。

There isn't necessarily a consistent character-by-character ordering for any particular locale.

MSDN文档

例如,培养可以指定字符的某些组合   被视为单个字符,或大写和小写字符   以特定方式进行比较,或一个字符的排列顺序   依赖于precede或按照它的字符。

For example, a culture could specify that certain combinations of characters be treated as a single character, or uppercase and lowercase characters be compared in a particular way, or that the sorting order of a character depends on the characters that precede or follow it.

,以确保一致的字符逐个字符排序的唯一方法是使用序号比较,这表现在<一个href="http://stackoverflow.com/questions/2244480/string-comparison-in-net-vs/2244531#2244531">Anton's回答。

The only way to ensure consistent character-by-character ordering is by using an ordinal comparison, as demonstrated in Anton's answer.

这篇关于在.net字符串比较:&QUOT; + QUOT; VS&QUOT; - &QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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