Erlang Trim Thu, 11 Feb 2010 13:30:13 -0500

This is a nice piece of code that I stumbled upon a few months ago while wanting to string the white space off the end of a string. Thanks go to Steve Davis for his contribution.


-module(trim).
-author('Steve Davis < steven · charles · davis ? gmail · com >').
-export([trim/1]).

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.


Comments:

  philipp Fri, 7 May 2010 04:15:59 -0400
hi, maybe this works for you aswell: 1> string:strip("something ", right, $\s). "something" 2> string:strip("something\n", right, $\n). "something" Cheers, Philipp...

  • About The Author
  • This is the definitive blog for the musings of Brian Smith. I've been a programmer / sys admin for most of my life. I don't have much to say, I work, I enjoy my family, and every once in awhile I'll dump a little something I want to keep track of here.

    Currently I am the CTO of DNS.com where we provide geolocation based authoritative DNS services for the masses. I also have been working on drafts that are submitted to the IETF to the dnsext working group.

    In the past I have been involved with organizing various user groups, including an off and on again 2600 group. Also I have been involved with the development of Seclude, an open source secure instant messaging platform as well as Sliker, a project that later developed into plasma for the KDE desktop environment.

    Additionally I am an avid home brewer making my own beer. Everything else should be below or to the left.

  • Disclaimer
  • The ideas and opinions expressed here are mine.
  • I'm a Linux and BSD user, and lean heavily toward the use of OSS vs certain other commercial solutions.

:= RSS =: