[英]How do I compare two strings together to see if one is greater than the other(alphabetically)? c#
How do I compare two strings together to see if one is greater than the other(alphabetically)? 如何比较两个字符串,以查看一个字符串是否大于另一个字符串(按字母顺序)? for example if I were to compare b and a, a would be greater than b because it comes first in the alphabet. 例如,如果我要比较b和a,则a会大于b,因为它在字母表中排在第一位。
This is what I am trying to compare: 这是我要比较的:
if (StringArray[lower] <= StringArray[middle])
You can use String.Compare
method. 您可以使用String.Compare
方法。
var control = string.Compare("a", "b") > 0;
This will return false
because b
is greater than a
, if you want to reverse it then change it like this: 这将返回false
因为b
大于a
,如果要反转它,请按如下所示更改它:
var control = string.Compare("a", "b") < 0;
please check : 请检查 :
{
string a = "a"; // 1
string b = "b"; // 2
int c = string.Compare(a, b);
Console.WriteLine(c);
c = string.CompareOrdinal(b, a);
Console.WriteLine(c);
c = a.CompareTo(b);
Console.WriteLine(c);
c = b.CompareTo(a);
Console.WriteLine(c);
}
Output 产量
-1 (This means a is smaller than b)
1 (This means b is smaller than a)
-1
1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.