将字符串与Erlang中的列表区分开来 [英] differentiate a string from a list in Erlang
问题描述
我的一部分代码需要确保某些东西不仅仅是一个列表,而是一个字符串。 (真正的字符串)。它需要分开列表,例如
[1,2,3,a,b,josh]
,来自字符串例如Muzaaya
。对于这两个字符串和列表,守卫表达式 is_list / 1
将会说 true
。没有像 is_string / 1
Muzaaya2536 618 Joshua
。我需要一个代码片段(Erlang),它将检查我,并确保该变量是一个字符串,而不仅仅是一个列表。谢谢您可以在模块 io_lib
中有两个功能有用的: io_lib:printable_list / 1
和 io_lib:printable_unicode_list / 1
,测试参数是否分别是可打印的 latin1 或 unicode 字符列表。
In Erlang when you have a list of printable characters, its a string, but a string is also a list of items and all functions of a list can be applied onto a string. Ideally, the data structure: string doesn't exist in erlang.
Part of my code needs to be sure that something is not only a list, but its a string. (A real string). It needs to separate lists e.g. [1,2,3,a,b,"josh"]
, from string e.g. "Muzaaya"
. The guard expression is_list/1
will say true
for both strings and lists. There is no such guard as and so this means i need a code snippet will make sure that my data is a string. A string in this case is a list of only printable (alphabetical, both cases, upper and lower), and may contain numbers e.g is_string/1
"Muzaaya2536 618 Joshua"
. i need a code snippet (Erlang) that will check for me and ensure that the variable is a string , not just a list. thanks
You have two functions in the module io_lib
which can be helpful: io_lib:printable_list/1
and io_lib:printable_unicode_list/1
which test if the argument is a list of printable latin1 or unicode characters respectively.
这篇关于将字符串与Erlang中的列表区分开来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!