嘗試這種結構:
re:replace(A, "\\s+", "", [global,{return,list}]).
實施例的會話:
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
上述溶液將去除內部字符串空間符號太(不僅前導和拖尾)。
如果你只需要剝離領導和拖尾,你可以使用這樣的事情:
re:replace(re:replace(A, "\\s+$", "", [global,{return,list}]), "^\\s+", "", [global,{return,list}]).
舉例會議:
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"
這不是「奇怪」,它被記錄爲只修剪*空格* aka空格:http://erlang.org/doc/man/string.html#strip-1。 – Tommy