在Erlang中区分字符串和列表

lskq00tm  于 2022-12-08  发布在  Erlang
关注(0)|答案(2)|浏览(262)

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. Really, 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 it's 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 is_string/1 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 "Muzaaya2536 618 Joshua" . I need a code snippet please (Erlang) that will check this for me and ensure that the variable is a string, not just a list. thanks

kdfy810k

kdfy810k1#

在模块io_lib中有两个函数可以帮助您:io_lib:printable_list/1io_lib:printable_unicode_list/1,分别测试参数是否为可打印的latin1unicode字符列表。

0tdrvxhp

0tdrvxhp2#

使用isprint(3)定义的可打印字符--

isprint(X) when X >= 32, X < 127 -> true;
isprint(_) -> false.

is_string(List) when is_list(List) -> lists:all(fun isprint/1, List);
is_string(_) -> false.

但你不能用它来保护自己。

相关问题