How to strip all blank characters in a string in Erlang?
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"
Try this:
re:replace(A, "(^\\s+)|(\\s+$)", "", [global,{return,list}]).