本文介绍了字符串比较“逻辑”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么下面的字符串比较提供这些结果?

  >1040" = =12000
True
>> 1040< =10000
False

在C和Python中,结果显然是正确的,我只是不知道如何计算结果...



PS:我知道比较字符串不同的长度是你不应该做的,但我仍然想知道上面这些行背后的逻辑;-)

解决方案

1等于1。



0在2之前(因此1040

4在0之后(因此1040>10000)。


Can anybody please tell me why the string comparisons below deliver these results?

>>"1040"<="12000"
True
>> "1040"<="10000"
False

I've tried the string comparison in both C and Python, the result is obviously correct, I just can't figure out how the result is calculated...

P.S.: I know that comparing strings of different length is something you shouldn't do, but I'm still wondering about the logic behind the above lines ;-)

解决方案

"1" is equal to "1".

"0" comes before "2" (so "1040" < "12000").

"4" comes after "0" (so "1040" > "10000").

这篇关于字符串比较“逻辑”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 22:41