[erlang-questions] splitting string into to substring

Richard A. O'Keefe ok@REDACTED
Thu Sep 11 06:22:59 CEST 2008


On 11 Sep 2008, at 10:19 am, Yunita C wrote:
> I am new to erlang and I want to write Erlang function that given a  
> string and a pattern and uses the pattern to split the string into  
> like this:

If the pattern is <x> then <a><x><b><x><c> turns into [<a>,<b>,<c>].

If only we had a function that would take <a><x><other stuff> and
give us {<a>,<other stuff>} or <a> (with no <x>) and give us {<a>}
we could write

split(List, Separator) ->
     case first_substring(List, Separator)
       of {First,Rest} -> [First | split(Rest, Separator)]
        ; {Only}       -> [Only]
     end.

Already we can see that split("", Sep) = [""] for any Sep.

If only we had a function that would tell us if one list was a
prefix of another, we could write the missing function:

first_substring(List, Separator) ->
     first_substring_loop(List, Separator, []).

first_substring_loop([], _, Reversed_First) ->
     {lists:reverse(Reversed_First)};
first_substring_loop(List = [Head|Tail], Separator, Reversed_First) ->
     case prefix_check(Separator, List)
       of no -> first_substring_loop(Tail, Separator, [Head| 
Reversed_First])
        ; Suffix -> {lists:reverse(Reversed_First), Suffix}
     end.

Writing prefix_check/2 isn't going to be hard.

prefix_check([], List) ->
     List;
prefix_check([Head|Prefix], [Head|List]) ->
     prefix_check(Prefix, List);
prefix_check(_, _) ->
     no.

I would have mentioned regexp:split/2, except that it gets
the separator " " (one space) wrong.  So I shan't.




More information about the erlang-questions mailing list