目录

Elixir - Char Lists( Char Lists)

char列表只不过是一个字符列表。 考虑以下程序来理解相同的内容。

IO.puts('Hello')
IO.puts(is_list('Hello'))

上述程序产生以下结果 -

Hello
true

char列表不包含字节,而是包含单引号之间字符的代码点。 So while the double-quotes represent a string (ie a binary), singlequotes represent a char list (ie a list) 。 请注意,如果任何字符超出ASCII范围,IEx将仅生成代码点作为输出。

Char列表主要在与Erlang接口时使用,特别是不接受二进制作为参数的旧库。 您可以使用to_string(char_list)和to_char_list(string)函数将char列表转换为字符串并返回

IO.puts(is_list(to_char_list("hełło")))
IO.puts(is_binary(to_string ('hełło')))

上述程序产生以下结果 -

true
true

NOTE - 函数to_stringto_char_list是多态的,即它们可以采用多种类型的输入,如原子,整数,并分别将它们转换为字符串和字符列表。

↑回到顶部↑
WIKI教程 @2018