我正试图在Javascript
中了解字符串比较function f(str){
return str[0] < str[str.length -1]
}
f("a+"); // false
在ASCII中:'a' == 97, '+' == 43
我在考虑测试时是否正确:f(str)
基于上面的ASCII值?
答案 0 :(得分:2)
您不需要为此分开拉弦的功能或复杂的测试。只需做bottom:-1%
并了解会发生什么。或者,更简单地说,使用'a' < '+'
检查char的charcode。
答案 1 :(得分:1)
你几乎是对的。它基于unicode代码单元(不是代码点,这是16位编码版本),而不是ascii值。
来自ECMAScript 2015 specification:
If both px and py are Strings, then
If py is a prefix of px, return false. (A String value p is a prefix of String value q if q can be the result of concatenating p and some other String r. Note that any String is a prefix of itself, because r may be the empty String.)
If px is a prefix of py, return true.
Let k be the smallest nonnegative integer such that the code unit at index k within px is different from the code unit at index k within py. (There must be such a k, for neither String is a prefix of the other.)
Let m be the integer that is the code unit value at index k within px.
Let n be the integer that is the code unit value at index k within py.
If m < n, return true. Otherwise, return false.
<强>注2 强>
字符串的比较使用简单的词典排序 代码单元值的序列。没有尝试使用更多 复杂的,面向语义的字符或字符串定义 Unicode规范中定义的相等和整理顺序。 因此,字符串值根据规范标准相等 Unicode标准可以测试为不相等。实际上这个算法 假设两个字符串都已经处于规范化形式。另外,请注意 对于包含增补字符的字符串,词典 UTF-16代码单元值序列的排序与上面的顺序不同 代码点值序列。
基本上它意味着字符串比较基于lexicographical order&#34;代码单元&#34;,这是unicode字符的数值。
答案 2 :(得分:0)
允许JavaScript引擎使用UCS-2或UTF-16(对于大多数实际用途来说都是一样的。)
因此,从技术上讲,您的功能基于UTF-16值,您正在比较NSURLSession
和0x0061
。