Erlang implements different functions to test if a list is a flat list in module io_lib. One good choice for checking your particulary IP strings is io_lib:latin1_char_list(Term) http://erlang.org/doc/man/io_lib.html#latin1_char_list-1 io_lib:latin1_char_list/1 function implementation is:
latin1_char_list([C|Cs]) when is_integer(C), C >= $\000, C =< $\377 ->
latin1_char_list(Cs);
latin1_char_list([]) -> true;
latin1_char_list(_) -> false.
If you want to test for flat unicode lists you can use io_lib:char_list(Term) http://erlang.org/doc/man/io_lib.html#char_list-1 io_lib:char_list/1 function implementation is:
char_list([C|Cs]) when is_integer(C), C >= 0, C < 16#D800;
is_integer(C), C > 16#DFFF, C < 16#FFFE;
is_integer(C), C > 16#FFFF, C =< 16#10FFFF ->
char_list(Cs);
char_list([]) -> true;
char_list(_) -> false.
Check the io_lib module documentation for other similar functions.
Notice that if some new erlang function are missing from your current project supported erlang version you can simply copy the implementation new erlang versions provides and add them into a module of your own. Search the latest erlang/lib/*/src source code and simply get the new functions you need.
4条答案
按热度按时间ergxz8rk1#
如何做很大程度上取决于你打算如何处理结果,或者更确切地说,取决于你打算如何做。所以,如果你对这些细节感兴趣:
或者,如果您对实际拆分字符串/字符串列表不感兴趣,您可以执行以下操作:
我理解的对吗?我没有写任何代码来检查字符串是否是字符串,这应该早一点完成。另一种方法是在生成字符串/字符串列表时,总是将其放入其中一种格式。
hjzp0vay2#
我有时会写这样的话:
cxfofazt3#
Erlang implements different functions to test if a list is a flat list in module io_lib.
One good choice for checking your particulary IP strings is io_lib:latin1_char_list(Term) http://erlang.org/doc/man/io_lib.html#latin1_char_list-1
io_lib:latin1_char_list/1 function implementation is:
If you want to test for flat unicode lists you can use io_lib:char_list(Term) http://erlang.org/doc/man/io_lib.html#char_list-1
io_lib:char_list/1 function implementation is:
Check the io_lib module documentation for other similar functions.
Notice that if some new erlang function are missing from your current project supported erlang version you can simply copy the implementation new erlang versions provides and add them into a module of your own. Search the latest erlang/lib/*/src source code and simply get the new functions you need.
nnt7mjpx4#
If this distinction needs to be determined in the function head, this is a way to figure this out in the guard:
Erlang characters are integers,
so, to check for string inputs:
Personally, I find the above version more intuitive than