如何删除字符串中间的空格

如题所述

JAVA中去掉空格
1. String.trim()
trim()是去掉首尾空格
2.str.replace(" ", ""); 去掉所有空格,包括首尾、中间
复制代码 代码如下:String str = " hell o ";
String str2 = str.replaceAll(" ", "");
System.out.println(str2);
3.或者replaceAll(" +",""); 去掉所有空格
温馨提示:答案为网友推荐,仅供参考
相似回答