How can i split a binary in erlang

后端 未结 5 1737
挽巷
挽巷 2020-12-31 14:57

What I want is, I think, relatively simple:

> Bin = <<\"Hello.world.howdy?\">>.
> split(Bin, \".\").
[<<\"Hello\">>, <<\"         


        
5条回答
  •  长情又很酷
    2020-12-31 15:06

    There is about 15% faster version of binary split working in R12B:

    split2(Bin, Chars) ->
        split2(Chars, Bin, 0, []).
    
    split2(Chars, Bin, Idx, Acc) ->
        case Bin of
            <> ->
                case lists:member(Char, Chars) of
                    false ->
                        split2(Chars, Bin, Idx+1, Acc);
                    true ->
                        split2(Chars, Tail, 0, [This|Acc])
                end;
            <> ->
                lists:reverse(Acc, [This])
        end.
    

    If you are using R11B or older use archaelus version instead.

    The above code is faster on std. BEAM bytecode only, not in HiPE, there are both almost same.

    EDIT: Note this code obsoleted by new module binary since R14B. Use binary:split(Bin, <<".">>, [global]). instead.

提交回复
热议问题