String 如何在Erlang中去掉字符串中的所有空白字符?
我知道在erlang中有string:strip。但它的行为对我来说很奇怪String 如何在Erlang中去掉字符串中的所有空白字符?,string,erlang,trim,strip,chomp,String,Erlang,Trim,Strip,Chomp,我知道在erlang中有string:strip。但它的行为对我来说很奇怪 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 我需要一个函数来删除所有前导/尾随空白字符,包括空格、\t、\n、\r等 som
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
我需要一个函数来删除所有前导/尾随空白字符,包括空格、\t、\n、\r等
some_module:better_strip(A) % => []
erlang是否有一个函数可以做到这一点?或者如果我必须自己做,最好的方法是什么?试试这个结构:
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"
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"
更新
上述解决方案也会去除字符串中的空格符号(不仅仅是前导和尾随)
如果您只需要剥离前导和尾翼,可以使用以下方法:
re:replace(re:replace(A, "\\s+$", "", [global,{return,list}]), "^\\s+", "", [global,{return,list}]).
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>
示例会话:
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"
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"
试试这个:
re:replace(A, "(^\\s+)|(\\s+$)", "", [global,{return,list}]).
使用内置函数:
string:strip/3
,可以获得一般抽象
clean(Text,Char)-> string:strip(string:strip(Text,right,Char),left,Char).
clean(Text,Char)->string:strip(string:strip(Text,right,Char),left,Char)。
您可以这样使用它:
re:replace(re:replace(A, "\\s+$", "", [global,{return,list}]), "^\\s+", "", [global,{return,list}]).
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>
Microsoft Windows[版本6.1.7601]
版权所有(c)2009微软公司。版权所有。
C:\Windows\System32>erl
Eshell V5.9(使用^G中止)
1> Clean=fun(Text,Char)->string:strip(string:strip(Text,right,Char),left,Char)结束。
#乐趣
2> 清洁(“Muzaaya”,32)。
“穆扎亚”
3> Clean(“--Erlang程序--”,$-)。
“Erlang程序”
4> Clean(Clean(“**WhatsApp在其后端使用Erlang和FreeBSD”,$*),32)。
“WhatsApp在其后端使用Erlang和FreeBSD”
5>
这是一个干净的方式,一般。字符必须是ASCII值。现在使用
字符串:trim
1> string:trim(" \t\n").
[]
以下是在erlang中执行此操作的简单方法:
145>A=“\t\n”。
“\t\n”
146>A1=[X | | X这也将删除非前导/尾随空格。是的。如果只需要去除前导和尾随符号,可以使用两种结构:替换(A,“^\\s+”,”“,[global,{return,list}])和替换(A,“\\s+$,”,[global,{return list}])谢谢。但我知道这一条,并写下我为什么不在问题中使用它。这并不“奇怪”,有文件证明它只修剪空格:。