我想要的是,我认为,相对简单:
> Bin = <<"Hello.world.howdy?">>.
> split(Bin, ".").
[<<"Hello">>, <<"world">>, <<"howdy?">>]
任何指针?
最佳答案
当前没有等效于 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.
关于erlang - 我如何在 erlang 中拆分二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/428124/