[英]What does "?\s" mean in Elixir?
在涵蓋理解的 Elixir 文檔中,我遇到了以下示例:
iex> for <<c <- " hello world ">>, c != ?\s, into: "", do: <<c>>
"helloworld"
我現在有點理解整個表達式,但我無法弄清楚“?\\ s”是什么意思。 我知道它以某種方式匹配並因此過濾掉了空格,但這就是我的理解結束的地方。
編輯:我現在已經發現它解析為 32,這是一個空格的字符代碼,但我仍然不知道為什么。
erlang具有由美元符號表示的字符文字。
Erlang/OTP 22 [erts-10.6.1] [...]
Eshell V10.6.1 (abort with ^G)
1> $\s == 32.
%%⇒ true
與elixir具有 char 文字的方式相同,根據代碼文檔,它與erlang char 文字完全一樣:
這正是 Erlang 對 Erlang 字符文字 ($a) 所做的。
基本上, ?\\s
與?
(問號后跟一個空格。)
# ⇓ space here
iex|1 ▶ ?\s == ?
warning: found ? followed by code point 0x20 (space), please use ?\s instead
?\\s
沒有什么特別之處,你可能會看到:
for <<c <- " hello world ">>, c != ?o, into: "", do: <<c>>
#⇒ " hell wrld "
此外, ruby也使用?c
符號表示字符文字:
main> ?\s == ' '
#⇒ true
?
是一個文字,它為您提供以下字符的代碼點( https://elixir-lang.org/getting-started/binaries-strings-and-char-lists.html#utf-8-and-unicode )。 對於無法按字面表達的字符(空格只是其中之一,但還有更多:制表符、回車符、...),應改用轉義序列。 所以?\\s
給你一個空間代碼點:
iex> ?\s
32
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.