Java中去除字符串中的空格

str.trim(); //去掉首尾空格

str.replace(" ",""); //去除所有空格,包括首尾、中间

str.replaceAll(" ", ""); //去掉所有空格,包括首尾、中间

str.replaceAll(" +","");  //去掉所有空格,包括首尾、中间str.replaceAll("\s*", ""); //可以替换大部分空白字符, 不限于空格 ;

原文地址:https://www.cnblogs.com/sengzhao666/p/11858567.html