Java字符串去掉空格的几种方法
作者:互联网
Method1:去掉首尾空格
String str = "Hello Genius! ";
System.out.println(str.trim());
Method2:去掉所有空格,包括首尾、中间
String str = "Hello Genius! ";
String str2 = str.replaceAll(" ","");
System.out.println(str2);
Method3:去掉所有空格,包括首尾、中间
String str = "Hello Genius! ";
String str3 = str.replaceAll("+","");
System.out.println(str3);
Method4:替换大部分空白字符,不限于空格
String str = "Hello Genius! ";
String str3 = str.replaceAll("\\s*","");
System.out.println(str3);
其中,\s可以匹配空格、制表符、换页符等空白字符
参考:Java正则表达式https://www.runoob.com/java/java-regular-expressions.html
标签:Java,String,System,空格,Genius,str,字符串,out 来源: https://blog.csdn.net/VioletHan7/article/details/99293607