Skip to content

Instantly share code, notes, and snippets.

@Siim
Created November 4, 2009 21:21
Show Gist options
  • Save Siim/226401 to your computer and use it in GitHub Desktop.
Save Siim/226401 to your computer and use it in GitHub Desktop.
erlang string tokenize
-module(split).
-export([tokens/2]).
-export([len/1]).
-export([substr/3]).
tokens(String, Limiter) ->
tokens(String, Limiter, [], []).
tokens([], _Limiter, Sub, Res) ->
[lists:reverse(Sub)|Res];
tokens(Str, Limiter, Sub, Res) ->
[H|T] = Str,
Substr = lists:sublist(Str,len(Limiter)),
if
Substr == Limiter ->
tokens(lists:sublist(Str,len(Limiter)+1, len(Str)), Limiter, [], [lists:reverse(Sub)|Res]);
true ->
tokens(T, Limiter, [H|Sub], Res)
end.
len(Str) ->
len(Str, 0).
len(Str,Len) ->
case Str of
[] -> Len;
[_|T] -> len(T,Len+1)
end.
substr(String, Start, End) ->
substr(String, Start, End, [], 1).
substr(String, Start, End, Res ,Count) ->
case String of
[] ->
lists:reverse(Res);
[H|T] ->
if
(Count >= Start) and (Start > 0) and (End >= Count) ->
substr(T, Start, End,[H|Res], Count+1);
true ->
substr(T, Start, End, Res, Count+1)
end
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment