java删除字符串中的空格、回车、换行符、制表符程序
java换行符
那是因为默认的情况下,界面上的换行时用的"n",这是LINUX和UNIX的换行符。在windows下换行符是"rn". 所以再将String对象写入到文件时,换行符应该把n写完整成rn
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4200')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4200>import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author lei
* 2011-9-2
*/
public class StringUtils {
public static String replaceBlank(String str) {
String dest = "";
if (str!=null) {
Pattern p = Pattern.compile("\s*|t|r|n");
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
public static void main(String[] args) {
System.out.println(StringUtils.replaceBlank("just do it!"));
}
/*-----------------------------------
笨方法:String s = "你要去除的字符串";
1.去除空格:s = s.replace('\s','');
2.去除回车:s = s.replace('n','');
这样也可以把空格和回车去掉,其他也可以照这样做。
注:n 回车(u000a)
t 水平制表符(u0009)
s 空格(u0008)
r 换行(u000d)*/
}
您可能感兴趣的文章:
java删除字符串中的空格、回车、换行符、制表符程序
php字符串函数有哪些
php字符串中去除左边第一逗号的方法
你真的了解strip()、lstrip()、rstrip()函数吗?
js去掉字符串的空格或换行符(附相关正则介绍)
php rtrim函数怎么用
php整理字符串方法
java中替换去除字符串中的空格/回车/换行符/制表符
js 禁止文本框输入空格的代码
js去掉空格的代码