c# - String.CompareTo 是如何工作的?

标签 c# string compare

当我比较包含正数/负数的字符串时,例如:

int res1 = "-1".CompareTo("1");
int res2 = "-1".CompareTo("2");

res1 equals 1.
res2 equals -1.

String.CompareTo 是如何工作的?这意味着它的顺序是“2 -1 1”...

最佳答案

来自 MSDN :

Certain nonalphanumeric characters might have special weights assigned to them. For example, the hyphen ("-") might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list.

编辑:忘了说了,这跟string.Compare使用的CompareOptions枚举有关。

关于c# - String.CompareTo 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1358659/

相关文章:

r - 计算R中两个数字的小数点后有多少位一致

php - 将 PHP 当前时间与 mysql 日期时间列进行比较

c# - 银光数据网格 : Copy & paste

c# - 如何从字符串中删除枚举?

c# - 在 FOR 循环中修改索引变量是一种好习惯吗?

java - 在同一行上向后打印

c# - C#无法正确比较两个日期时间

c# - 发送两个文本框而不是动态发送一个

java - java中的粗体字符串而不是将该字符串发送到打印机

java - SWIG从String作为Java中的String数组获取returntype