1 String ss = " happy new year "; 2 System.out.println(ss + "------" + ss.length()); 3 //" happy new year ------16" 4 System.out.println(ss.trim() + "------" + ss.trim().length()); 5 //"happy new year------14" 6 System.out.println(ss.replaceAll(" ", "") + "------" + ss.replaceAll(" ", "").length()); 7 //" happynewyear------13" 8 System.out.println(ss.replaceAll("\\s*", "") + "------" + ss.replaceAll("\\s*", "").length()); 9 //"happynewyear------12" \s在正则里就表示空白符