我想要的东西,我认为相对简单:
> Bin = <<"Hello.world.howdy?">>.
> split(Bin, ".").
[<<"Hello">>, <<"world">>, <<"howdy?">>]
有什么指针吗?
binary:split(Bin,<<".">>).
在R12B版本中,有一个大约比二分法更快15%的版本:
split2(Bin, Chars) ->
split2(Chars, Bin, 0, []).
split2(Chars, Bin, Idx, Acc) ->
case Bin of
<<This:Idx/binary, Char, Tail/binary>> ->
case lists:member(Char, Chars) of
false ->
split2(Chars, Bin, Idx+1, Acc);
true ->
split2(Chars, Tail, 0, [This|Acc])
end;
<<This:Idx/binary>> ->
lists:reverse(Acc, [This])
end.
binary:split(Bin, <<".">>, [global])
。目前没有与lists:split/2
等效的OTP函数可用于二进制字符串。在EEP-9公开之前,您可以编写类似于以下的二进制拆分函数:
split(Binary, Chars) ->
split(Binary, Chars, 0, 0, []).
split(Bin, Chars, Idx, LastSplit, Acc)
when is_integer(Idx), is_integer(LastSplit) ->
Len = (Idx - LastSplit),
case Bin of
<<_:LastSplit/binary,
This:Len/binary,
Char,
_/binary>> ->
case lists:member(Char, Chars) of
false ->
split(Bin, Chars, Idx+1, LastSplit, Acc);
true ->
split(Bin, Chars, Idx+1, Idx+1, [This | Acc])
end;
<<_:LastSplit/binary,
This:Len/binary>> ->
lists:reverse([This | Acc]);
_ ->
lists:reverse(Acc)
end.
re:split(<<"Hello.world.howdy?">>, "\\.").