您的位置:首页 > 编程语言 > Java开发

java去除字符串中的空格、回车、换行符、制表符

2013-07-09 16:44 232 查看


1.[代码]java去除字符串中的空格、回车、换行符、制表符

01
import
java.util.regex.Matcher;
02
import
java.util.regex.Pattern;
03
04
05
06
/**
07
*
@authorlei
08
*
2011-9-2
09
*/
10
public
class
StringUtils
{
11
12
public
static
String
replaceBlank(Stringstr){
13
String
dest=
""
;
14
if
(str!=
null
)
{
15
Pattern
p=Pattern.compile(
"\\s*|\t|\r|\n"
);
16
Matcher
m=p.matcher(str);
17
dest
=m.replaceAll(
""
);
18
}
19
return
dest;
20
}
21
public
static
void
main(String[]
args){
22
System.out.println(StringUtils.replaceBlank(
"just
doit!"
));
23
}
24
/*-----------------------------------
25
26
笨方法:String
s="你要去除的字符串";
27
28
1.去除空格:s
=s.replace('\\s','');
29
30
2.去除回车:s
=s.replace('\n','');
31
32
这样也可以把空格和回车去掉,其他也可以照这样做。
33
34
注:\n
回车(\u000a)
35
\t
水平制表符(\u0009)
36
\s
空格(\u0008)
37
\r
换行(\u000d)*/
38
}
2.

publicclassT3{
publicstaticvoidmain(Stringargs[]){
Stringstr="aabbcc";
System.out.println(str.replaceAll("",""));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: