11

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?

halfelf
  • 9,737
  • 13
  • 54
  • 63
  • It isn't "strange", it is documented that it only trims *blanks* aka spaces: http://erlang.org/doc/man/string.html#strip-1. – Tommy May 16 '17 at 14:55

5 Answers5

18

Try this:

re:replace(A, "(^\\s+)|(\\s+$)", "", [global,{return,list}]).
Tilman
  • 2,015
  • 14
  • 16
9

Try this construction:

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

Example session:

Erlang R15B01 (erts-5.9.1) [source] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9.1  (abort with ^G)
1> A = "  21\t\n  ".
"  21\t\n  "
2> re:replace(A, "\\s+", "", [global,{return,list}]).
"21"

UPDATE

Above solution will strip space symbols inside string too (not only leading and tailing).

If you need to strip only leading and tailing, you can use something like this:

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

Example session:

Erlang R15B01 (erts-5.9.1) [source] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9.1  (abort with ^G)
1> A=" \t \n 2 4 \n \t \n  ".
" \t \n 2 4 \n \t \n  "
2> re:replace(re:replace(A, "\\s+$", "", [global,{return,list}]), "^\\s+", "", [global,{return,list}]).
"2 4"
fycth
  • 3,410
  • 25
  • 37
  • 1
    This will delete also non-leading/trailing whitespace. – Tilman Oct 09 '12 at 08:00
  • 1
    Yes. If you need to strip leading and tailing symbols only, you can use two constructions 're:replace(A, "^\\s+", "", [global,{return,list}]).' and 're:replace(A, "\\s+$", "", [global,{return,list}]).' – fycth Oct 09 '12 at 08:21
8

Nowadays use string:trim

1> string:trim("  \t\n").
[]
N. Hoogervorst
  • 146
  • 1
  • 5
1

Using a built in function: string:strip/3, you can have a general abstraction

clean(Text,Char)-> string:strip(string:strip(Text,right,Char),left,Char).
The you would use it like this:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Windows\System32>erl
Eshell V5.9  (abort with ^G)
1> Clean = fun(Text,Char) -> string:strip(string:strip(Text,right,Char),left,Char) end.
#Fun<erl_eval.12.111823515>
2> Clean(" Muzaaya   ",32).
"Muzaaya"
3> Clean("--Erlang Programs--",$-).
"Erlang Programs"
4> Clean(Clean("** WhatsApp Uses Erlang and FreeBSD in its Backend  ",$*),32).
"WhatsApp Uses Erlang and FreeBSD in its Backend"
5> 

This is a clean way, and general. The Char must be an ASCII value.

Muzaaya Joshua
  • 7,736
  • 3
  • 47
  • 86
1

Here is the short way of doing it in erlang:

145> A = "  \t\n".
"  \t\n"
146> A1 = [X || X<-A, X =/= 32].
"\t\n"

The 32 above represents a ' ' character.

Locke
  • 7,626
  • 2
  • 21
  • 41