a) // suppose to-6ren">
gpt4 book ai didi

c# - 无法比较字符串 > 字符串

转载 作者:行者123 更新时间:2023-11-30 19:06:09 25 4
gpt4 key购买 nike

我正在尝试做这样的事情:

  // some code...

string a="aa";
string b="bb";
// ...
if(b>a) // suppose to be true
{
// do somthing
}

但是我得到了编译错误:

Operator '>' cannot be applied to operands of type 'string' and 'string'.

那么最好的方法是什么?

最佳答案

使用 CompareTo() 方法代替。

if(b.CompareTo(a) > 0)

Compares two specified String objects and returns an integer that indicates their relative position in the sort order.

这个方法;

  • if b is greater than a or a is null, it returns greater than zero.
  • if b is less than a, it returns less than zero.
  • if b has the same position in the sort order as a, it returns 0.

检查这个 DEMO .

关于c# - 无法比较字符串 > 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14317814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com