String Compare “Logic”

前端 未结 11 1618
悲哀的现实
悲哀的现实 2020-12-06 19:06

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

>>\"1040\"<=\"12000\"  
True  
>> \"1040\"<=\"1         


        
11条回答
  •  死守一世寂寞
    2020-12-06 19:31

    It compares the "numbers" on a character by character basis. In the first case, "1" == "1", but then "0" < "2" in ASCII (and as an integer) so it returns true.

    In the second case, 1==1, 0==0, but 4 > 0, so it returns false.

    And there's nothing wrong with comparing strings of a different length... but you should use the appropriate string comparison method.

提交回复
热议问题