[erlang-questions] splitting string into to substring

Søren Hilmer <>
Thu Sep 11 10:37:45 CEST 2008


What about something like this:

-module(split).

-export([split/2]).

split(S,P) ->
    split_h (S,P,[]).

split_h([],_P,H) -> {lists:reverse(H), []};
split_h(S,P,H) ->
	case lists:prefix(P,S) of
	true -> {lists:reverse(H), lists:nthtail(length(P),S)};
	false -> [A|S2] = S, split_h(S2,P,[A|H])
   end.
-- 
Søren Hilmer, M.Sc., M.Crypt.
wideTrail            Phone: +45 25481225
Pilevænget 41        Email: 
DK-8961  Allingåbro  Web: www.widetrail.dk

On Thu, September 11, 2008 00:19, Yunita C wrote:
> Hi All,
>
>
>
>
>
> 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:
>
>
>
>
> For example if the pattern is "ab" and the string is "acabcabts" the code
> will return ["ac", "cabts"].
>
>
>
>
>
> If the pattern is "bc" and the string is "bcad" the code returns ["", ad].
>
>
>
>
>
> If the pattern is "ac" and the string is "bcad" the code will return
> ["bcad", ""].
>
>
>
>
>
> I'm trying to make function using  list but still stuck there. Can anybody
> help me? Thank's so much.
>
>
>       _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions





More information about the erlang-questions mailing list