文档结构  
翻译进度:已翻译     翻译赏金:0 元 (?)    ¥ 我要打赏

今天的Erlang Thursday讲的是  string:tokens/2.

string:tokens/2 第一个入参是一个字符串,第二入参是一个分割字符列表,返回值是被分割后的字符串列表。

string:tokens("foo", "").
% ["foo"]
string:tokens("banana", "a").
% ["b","n","n"]
string:tokens("It was the best of times, it was the worst of times", " ").
% ["It","was","the","best","of","times,","it","was","the",
%  "worst","of","times"]

当连续的分割字符出现在字符串里,它们将被当做一个分割字符,并且没有空字符串返回。

第 1 段(可获 2 积分)
string:tokens("Mississippi", "s").
% ["Mi","i","ippi"]
65> string:tokens("Mississippi", "sp").
% ["Mi","i","i","i"]
string:tokens("Mississippi", "is").
% ["M","pp"]

传个 string:tokens/2 的分割字符列表的字符顺序无关紧要,可以按任意顺序指定。

string:tokens("Mississippi", "ps").
% ["Mi","i","i","i"]
65> string:tokens("Mississippi", "sp").
% ["Mi","i","i","i"]

同时由于分隔符列表只是简单的一个分割字符串列表,我们可以不是传递一个字符串,而是把字符的整数值组成的列表当做一个列表传给函数,因为一个整数值的列表和字符串是一样的。

第 2 段(可获 2 积分)
$s.
% 115
$p.
% 112
[115, 112].
% "sp"
string:tokens("Mississippi", [115]).
% ["Mi","i","ippi"]
string:tokens("Mississippi", [115, 112]).
% ["Mi","i","i","i"]

第 3 段(可获 2 积分)

文章评论