Java中去除字符串中全部空格的几种方法

JAVA中去掉空格   

1. String.trim()  

trim()是去掉首尾空格  

2.str.replaceAll(" ", ""); 去掉全部空格,包括首尾、中间  java

String str = " hell o ";  
String str2 = str.replaceAll(" ", "");  
System.out.println(str2);

3.或者replaceAll(" +",""); 去掉全部空格  

4.str = .replaceAll("\\s*", "");  
能够替换大部分空白字符, 不限于空格   
\s 能够匹配空格、制表符、换页符等空白字符的其中任意一个 code

相关文章
相关标签/搜索