试试这个结构:
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"
更新
上述解决方案也会在字符串中去除空格符号(不仅是前导和尾随)。
如果您只需要去除前导和尾随,则可以使用以下内容:
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"