Javascript '>' 运算符如何将字符与空格进行比较?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15293458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 00:15:13  来源:igfitidea点击:

How does the Javascript '>' operator compare characters with a space?

javascript

提问by Guilherme Longo

I am trying to understand this expression:

我试图理解这个表达:

((ch = stream.getChar()) > ' ')

Here, getChar()gets a character. How does this greater-than comparision operator check if any char is greater than an empty space?

在这里,getChar()得到一个字符。这个大于比较运算符如何检查是否有任何字符大于空格?

Is this possible?

这可能吗?

回答by christopher

An empty space has a character code. Even though it doesn't look like much, it still has a value. So does the character taken from the stream. Comparing the character codes of these values is what produces the output.

空白处有一个字符代码。虽然看起来不多,但还是有一定的价值的。从流中提取的字符也是如此。比较这些值的字符代码是产生输出的原因。

回答by Zirak

Let's take a gander at the language specification(the algorithm itself is described in here) (do note that it defines <, but the >operator simply flips the resulting value).

让我们看一下语言规范(算法本身在此处描述)(请注意它定义了<,但>运算符只是翻转结果值)。

What the operator does is try to convert both operands to primitive types, with a preference for numbers:

运算符所做的是尝试将两个操作数转换为原始类型,并优先考虑数字:

2. a. Let py be the result of calling ToPrimitive(y, hint Number).
2. b. Let px be the result of calling ToPrimitive(x, hint Number).

In our case, x === stream.getChar()and y === ' '. Since both of the operands are primitive strings already, that results in the original values (px = x, py = y), and we move on to:

在我们的例子中,x === stream.getChar()y === ' '。由于两个操作数都已经是原始字符串,因此会产生原始值 ( px = x, py = y),我们继续:

4. Else, both px and py are Strings

Now it does checks to see if any of the operands are prefixes of the other, for example:

现在它会检查是否有任何操作数是另一个操作数的前缀,例如:

'abc' > 'abcd' // false
'foo' > 'foobar' // false

Which is relevant if getChar()results in a space, since the space is a prefix of itself:

如果getChar()结果为空格,这是相关的,因为空格是其自身的前缀:

' ' > ' ' // false

We move on, to finding the first character in xand ywho're on the same position in the strings, but are different characters:

我们继续寻找第一个字符 inxywho 在字符串中的相同位置,但不同的字符:

Let k be the smallest nonnegative integer such that the character at position k within px is different from the character at position k within py. (There must be such a k, for neither String is a prefix of the other.)

设 k 是最小的非负整数,使得 px 中位置 k 处的字符与 py 中位置 k 处的字符不同。(必须有这样的 ak,因为 String 都不是另一个的前缀。)

(e.g., 'efg'and 'efh', we want gand h)

(例如, 'efg'and 'efh', 我们想要gand h)

The characters we've found are then converted to their integer values:

我们找到的字符然后被转换为它们的整数值:

Let m be the integer that is the code unit value for the character at position k within px.
Let n be the integer that is the code unit value for the character at position k within py.

And finally, a comparison is made:

最后,进行比较:

If m < n, return true. Otherwise, return false.

And that's how it's compared to the space.

这就是它与空间相比的方式。



tl;dr It converts both arguments to their code-unit integer representations, and compares that.

tl;dr 它将两个参数转换为它们的代码单元整数表示,并进行比较。

回答by Alberto Segura

In Javascript strings are compared in alphabetical order. These expressions are true:

在 Javascript 中,字符串按字母顺序进行比较。这些表达是正确的:

 'abacus' <= 'calculator'
 'abacus' < 'abate'

回答by Jamiec

In most (if not all) programming languages, characters are represented internally by a number. When you do equality/greater-than/less-than checks what you're actually checking is the underlying number.

在大多数(如果不是全部)编程语言中,字符在内部由数字表示。当您执行相等/大于/小于检查时,您实际检查的是基础数字。

hence in JS:

因此在 JS 中:

alert('c' > 'b'); // alerts true
alert('a' > 'b'); // alerts false

A space character also has a numeric representation, therefore the check is a valid one.

空格字符也有数字表示,因此检查是有效的。

回答by simplyray

[string] > [string]will compare the character(s) by their representative values (see ASCII Table)

[string] > [string]将按字符的代表值比较字符(见ASCII 表

回答by Gilad Naaman

Characters are stored in the computer's memory as a number (usually a byte or two).

字符以数字(通常是一两个字节)的形式存储在计算机的内存中。

Each character has a unique identifying number.

每个字符都有一个唯一的标识号。

By checking if a character is greater than space, you actually comapare their place in a table.

通过检查字符是否大于空格,您实际上将它们在表中的位置进行了比较。

See http://en.wikipedia.org/wiki/ASCIIfor more.

有关更多信息,请参阅http://en.wikipedia.org/wiki/ASCII

回答by Deleteman

Check out this link, it'll explain how the comparison works on JS: http://javascript.about.com/od/decisionmaking/a/des02.htmBasically, you're comparing the ASCII value of each character to the ASCII value of the blank space, which is also, a character and therefore, has a corresponding ASCII value.

查看此链接,它将解释比较如何在 JS 上工作:http: //javascript.about.com/od/decisionmaking/a/des02.htm基本上,您将每个字符的 ASCII 值与 ASCII 进行比较空格的值,它也是一个字符,因此具有相应的 ASCII 值。