How to strip all blank characters in a string in Erlang?

halfelf picture halfelf · Oct 9, 2012 · Viewed 10.4k times · Source

I know there is string:strip in erlang. But its behaviour is strange for me.

A = "  \t\n"  % two whitespaces, one tab and one newline
string:strip(A)   % => "\t\n"
string:strip(A,both,$\n)  %  string:strip/3 can only strip one kind of character

And I need a function to delete all leading/trailing blank chars, including whitespace, \t, \n, \r etc.

some_module:better_strip(A)    % => []

Does erlang have one function can do this? Or if I have to do this myself, what is the best way?

Answer

Tilman picture Tilman · Oct 9, 2012

Try this:

re:replace(A, "(^\\s+)|(\\s+$)", "", [global,{return,list}]).