Java_去除字符串空格

时间:2023-03-10 02:09:24
Java_去除字符串空格
 String ss = "    happy new year ";
System.out.println(ss + "------" + ss.length());
//" happy new year ------16"
System.out.println(ss.trim() + "------" + ss.trim().length());
//"happy new year------14"
System.out.println(ss.replaceAll(" ", "") + "------" + ss.replaceAll(" ", "").length());
//" happynewyear------13"
System.out.println(ss.replaceAll("\\s*", "") + "------" + ss.replaceAll("\\s*", "").length());
//"happynewyear------12"  \s在正则里就表示空白符