String Compare “Logic”

前端 未结 11 1598
悲哀的现实
悲哀的现实 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:43

    how about making them the same length?

    That would unify numbers and alphas

    1040 becomes 01040

    01040 < 12000 now it makes sense

    maybe that is why he felt it was wrong to compare strings of different length when the strings are numbers they should be the same length

    0 讨论(0)
  • 2020-12-06 19:49

    Think alphabetized.

    0 讨论(0)
  • 2020-12-06 19:49

    You're experiencing lexicographical ordering.

    There are some generalized algorithms for this ordering in the book Elements of Programming. Search for the word lexicographical.

    0 讨论(0)
  • 2020-12-06 19:49

    To expand on the John P's answer, think of the strings as words, and read them left-to-right.

    To look at it another way,

    BAEA would come before BCAAA but after BAAAA

    0 讨论(0)
  • 2020-12-06 19:52

    The strings are compared, one character at a time, from left to right:

    10000
    1040
    12000
    

    There's nothing wrong with comparing strings of different lengths.

    0 讨论(0)
提交回复
热议问题