Smart way to trim a binary?

Steve Davis steven.charles.davis@REDACTED
Wed Jun 24 02:21:45 CEST 2009


I have this feeling that there's a much smarter way to trim a string
presented as a binary rather than this (mostly found in couch_util)...

trim(Bin) when is_binary(Bin) ->
	list_to_binary(trim(binary_to_list(Bin)));
trim(String) when is_list(String) ->
    String2 = lists:dropwhile(fun is_whitespace/1, String),
    lists:reverse(lists:dropwhile(fun is_whitespace/1, lists:reverse
(String2))).

is_whitespace($\s)-> true;
is_whitespace($\t)-> true;
is_whitespace($\n)-> true;
is_whitespace($\r)-> true;
is_whitespace(_Else) -> false.

...anybody have a nice way of doing this?

TIA & BR,
/sd


More information about the erlang-questions mailing list