两种方法 去除字符串中空格制表符换行:
public String checkString(String str) {
if (TextUtils.isEmpty(str)) return "";
int len = str.length();
int i = 0, j = 0;
char[] strChar = str.toCharArray();
for (; i < len; i++) {
if (' ' == strChar[i] || '\t' == strChar[i] || '\n' == strChar[i])
continue;
if (i != j) strChar[j] = strChar[i];
j++;
}
strChar[j] = 0;
return new String(Arrays.copyOf(strChar, j));
}
利用正则表达式
public static String replaceBlank(String src) {
String dest = "";
if (src != null) {
Pattern pattern = Pattern.compile("\t|\r|\n|\\s*");
Matcher matcher = pattern.matcher(src);
dest = matcher.replaceAll("");
}
return dest;
}
转载地址:http://blog.csdn.net/iblade/article/details/77651718
学习之路从来都没有捷径,我们一起朝着高手之路进发。