我想,我想要的只是相对简单:
> Bin = <<"Hello.world.howdy?">>.
> split(Bin, ".").
[<<"Hello">>, <<"world">>, <<"howdy?">>]
任何指针?
答案 0 :(得分:19)
binary:split(Bin,<<".">>).
答案 1 :(得分:9)
来自binary(和EEP31)的模块EEP9已添加到Erts-5.8中(请参阅OTP-8217):
1> Bin = <<"Hello.world.howdy?">>.
<<"Hello.world.howdy?">>
2> binary:split(Bin, <<".">>, [global]).
[<<"Hello">>,<<"world">>,<<"howdy?">>]
答案 2 :(得分:4)
在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.
如果您使用R11B或更早版本,请使用archaelus version。
上面的代码在std上更快。仅BEAM字节码,而不是HiPE,两者几乎相同。
编辑:请注意此代码自R14B以来被新模块binary淘汰。请改用binary:split(Bin, <<".">>, [global]).
。
答案 3 :(得分:3)
这是一种方式:
re:split(<<"Hello.world.howdy?">>, "\\.").
答案 4 :(得分:3)
当前的OTP函数没有等效于lists:split/2
的二进制字符串。在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.