在 Erlang 中将字符串转换为字符的最惯用的方法是什么?

What is the most idiomatic way to convert a string to characters in Erlang?

在 Erlang 中将此 "helloworld" 转换为 ["h","e","l","l","o","w","o","r","l","d"] 的最惯用方法是什么?

您可以尝试使用以下代码:

1> [[X] || X <- "helloworld"].
["h","e","l","l","o","w","o","r","l","d"]

该字符串是一个字符列表

1> [$h, $e, $l, $l, $o, $w, $o, $r, $l, $d].
"helloworld"

所以如果你问

What is the most idiomatic way to convert a string to characters in Erlang?

答案是none,已经是字符列表,不用再转换了

如果你问,如何对字符串的字符应用一些函数,例如如何减去 32。

2> [ X - 32 || X <- "helloworld" ].
"HELLOWORLD"

或者如果你问如何得到一个字符串的列表

3> [[X] || X <- "helloworld"].
["h","e","l","l","o","w","o","r","l","d"]

在这个简单的例子中,列表理解的替代方法是列表模块中的映射函数:

String = "helloworld",
lists:map(fun(X) -> [X] end, String).