本文介绍了为什么是字符串“11”小于字符串“3”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
if ('11' < '3') alert('true');
很明显,它不是通过长度来比较它们,而是通过编码。但是,我不明白它是如何工作的。我需要一些解释: - )
It's obvious that it's not comparing them by length but by encoding instead. However, I don't understand how it works. I need some explanation :-)
推荐答案
字符串逐个字符比较,直到它们不相等或没有任何字符左对比。 11的第一个字符小于3的第一个字符。
Strings are compared character by character until they are not equal or there aren't any characters left to compare. The first character of '11' is less than the first character of '3'.
> '11' < '3'
true
> '31' < '3'
false
> '31' < '32'
true
> '31' < '30'
false
如果我们使用字母,则 b
不小于 a
, abc
不小于 aaa
,但由于 c
小于 d
, abc
小于 abd
。
If we use letters then, since b
is not less than a
, abc
is not less than aaa
, but since c
is less than d
, abc
is less than abd
.
> 'abc' < 'aaa'
false
> 'abc' < 'abd'
true
这篇关于为什么是字符串“11”小于字符串“3”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!