正文:
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
本文详细介绍了在Java中去除字符串空格的各种方法,包括使用trim(), replace(), replaceAll()函数,以及自定义函数remove()。从简单去除首尾空格到复杂的所有空格清除,甚至针对不同空白字符的处理技巧,一网打尽。
360

被折叠的 条评论
为什么被折叠?



