正文:
1,去掉首尾空格 str.trim()
2,去掉所有空格① replace(" ", "")
3,去掉所有空格 ② replaceAll(" +","")
4,替换大部分空白字符, s 可以匹配空格、制表符、换页符等空白字符的其中任意一个 replaceAll("\s*", "")
5,代码去除空格
public String remove(String resource,char ch) { StringBuffer buffer=new StringBuffer(); int position=0; char currentChar; while(position<resource.length()) { currentChar=resource.charAt(position++); if(currentChar!=ch) buffer.append(currentChar);
}
return buffer.toString(); }
参考博客:
JAVA中去掉空格 - Alamps - 博客园
https://www.cnblogs.com/alamps/archive/2012/04/27/2473694.html