C# 字符串比较和单个字符的字母顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14967462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:35:28  来源:igfitidea点击:

String Comparison And Alphabetic Order of Individual Characters

c#stringstring-comparison

提问by Alexandar

I have a question related to string comparison vs. character comparison.

我有一个与字符串比较与字符比较相关的问题。

Characters >and 0(zero) have following decimal values 62and 48accordingly.

字符>0(零)具有以下十进制值6248因此。

When I compare two characters in the following code, I get value True(which is correct)

当我比较以下代码中的两个字符时,我得到了值True(这是正确的)

Console.WriteLine('>' > '0');

When I compare two one-character strings in the following code, I get value -1which indicates that ">" is less than "0"(default culture is English)

当我比较以下代码中的两个单字符字符串时,我得到的值-1表明“>”小于“0”(默认区域性为英语)

Console.WriteLine(string.Compare(">", "0"));

Whereas comparison of "3" and "1" (51and 49code values) in the following code returns 1(as expected)

而以下代码中“3”和“1”(5149代码值)的比较返回1(如预期)

Console.WriteLine(string.Compare("3", "1"));

Also, string.Compare(string str1, string str2)documentation says:

另外,string.Compare(string str1, string str2)文档说:

The comparison uses the current culture to obtain culture-specific information such as casing rules and the alphabetic order of individual characters

比较使用当前区域性来获取特定于区域性的信息,例如大小写规则和单个字符的字母顺序

Would you be able to explain (or provide reference to some documentation) how string comparison is implemented e.g. how alphabetic order of individual charactersis calculated etc?

您能否解释(或提供对某些文档的参考)如何实现字符串比较,例如如何计算单个字符的字母顺序等?

采纳答案by Pete Kirkham

When you compare the characters '>'and '0', you are comparing their ordinal values.

当您比较字符'>'和 时'0',您是在比较它们的序数值。

To get the same behaviour from a string comparison, supply the ordinal string comparison type:

要从字符串比较中获得相同的行为,请提供序数字符串比较类型:

  Console.WriteLine(string.Compare(">", "0", StringComparison.Ordinal));
  Console.WriteLine(string.Compare(">", "0", StringComparison.InvariantCulture));
  Console.WriteLine(string.Compare(">", "0", StringComparison.CurrentCulture));

The current culture is used by default, which has a sorting order intended to sort strings 'alphabetically' rather in strictly lexical order, for some definition of alphabetically.

默认情况下使用当前区域性,它的排序顺序旨在“按字母顺序”而不是严格按词法顺序对字符串进行排序,以用于某些字母顺序定义。

回答by DiskJunky

it returns -1because it is comparing str2to str1, not the other way around. Eg, "is 48 equal to 62". No, it's less than 62 so it returns -1. It's semantically a little confusing when you read the parameter order

它返回-1,因为它比较str2str1,而不是周围的其他方法。例如,“48 等于 62”。不,它小于 62,所以它返回 -1。当您阅读参数顺序时,在语义上有点混乱

回答by dtb

The sort order of strings depends on the culture you use.

字符串的排序顺序取决于您使用的区域性。

StringComparer.CurrentCulturesorts the following 1-character strings as follows on my machine:

StringComparer.CurrentCulture在我的机器上按如下方式对以下 1 个字符的字符串进行排序:

' -   ! " # $ % & (  ) * , . / : ; ? @ [
\ ] ^ _ ` { | } ~ +  < = > 0 1 2 3 4 5 6
7 8 9 a A b B c C d  D e E f F g G h H i
I j J k K l L m M n  N o O p P q Q r R s
S t T u U v V w W x  X y Y z Z

StringComparer.Ordinalsorts the same strings as follows:

StringComparer.Ordinal对相同的字符串进行排序,如下所示:

  ! " # $ % & ' ( )  * + , - . / 0 1 2 3
4 5 6 7 8 9 : ; < =  > ? @ A B C D E F G
H I J K L M N O P Q  R S T U V W X Y Z [
\ ] ^ _ ` a b c d e  f g h i j k l m n o
p q r s t u v w x y  z { | } ~

回答by Oren Melzer

It sounds like what you want is the comparison to not use culture-specific rules. Have you tried StringComparison.Ordinal:

听起来您想要的是不使用特定于文化的规则的比较。您是否尝试过 StringComparison.Ordinal:

Console.WriteLine( string.Compare( ">", "0", StringComparison.Ordinal ) ); // returns a positive number