[英]What does "?\s" mean in Elixir?
In the Elixir-documentation covering comprehensions I ran across the following example:在涵盖理解的 Elixir 文档中,我遇到了以下示例:
iex> for <<c <- " hello world ">>, c != ?\s, into: "", do: <<c>>
"helloworld"
I sort of understand the whole expression now, but I can't figure out what the "?\\s" means.我现在有点理解整个表达式,但我无法弄清楚“?\\ s”是什么意思。 I know that it somehow matches and thus filters out the spaces, but that's where my understanding ends.
我知道它以某种方式匹配并因此过滤掉了空格,但这就是我的理解结束的地方。
Edit: I have now figured out that it resolves to 32, which is the character code of a space, but I still don't know why.编辑:我现在已经发现它解析为 32,这是一个空格的字符代码,但我仍然不知道为什么。
erlang has char literals denoted by a dollar sign. erlang具有由美元符号表示的字符文字。
Erlang/OTP 22 [erts-10.6.1] [...]
Eshell V10.6.1 (abort with ^G)
1> $\s == 32.
%%⇒ true
The same way elixir has char literals that according to the code documentation act exactly as erlang char literals:与elixir具有 char 文字的方式相同,根据代码文档,它与erlang char 文字完全一样:
This is exactly what Erlang does with Erlang char literals ($a).
这正是 Erlang 对 Erlang 字符文字 ($a) 所做的。
Basically, ?\\s
is exactly the same as ?
基本上,
?\\s
与?
(question mark followed by a space.) (问号后跟一个空格。)
# ⇓ space here
iex|1 ▶ ?\s == ?
warning: found ? followed by code point 0x20 (space), please use ?\s instead
There is nothing special with ?\\s
, as you might see: ?\\s
没有什么特别之处,你可能会看到:
for <<c <- " hello world ">>, c != ?o, into: "", do: <<c>>
#⇒ " hell wrld "
Also, ruby as well uses ?c
notation for char literals:此外, ruby也使用
?c
符号表示字符文字:
main> ?\s == ' '
#⇒ true
?
is a literal that gives you the following character's codepoint( https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#utf-8-and-unicode ).是一个文字,它为您提供以下字符的代码点( https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#utf-8-and-unicode )。 For characters that cannot be expressed literally (space is just one of them, but there are more: tab, carriage return, ...) the escaped sequence should be used instead.
对于无法按字面表达的字符(空格只是其中之一,但还有更多:制表符、回车符、...),应改用转义序列。 So
?\\s
gives you a codepoint for space:所以
?\\s
给你一个空间代码点:
iex> ?\s
32
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.