java去除字串中的空格\t、回車\n、換行符\r、製表符\t
阿新 • • 發佈:2019-01-25
public class StringUtils {
//第一種方法 public static String replaceBlank(String str) { String dest = ""; if (str!=null) { Pattern p = Pattern.compile("\\s*|\t|\r|\n"); Matcher m = p.matcher(str); dest = m.replaceAll(""); } return dest; } public static void main(String[] args) { System.out.println(StringUtils.replaceBlank("just do it!")); } /*----------------------------------- //第二種方法:
String s = "總字串";
1.去除空格:s = s.replace('\\s','');
2.去除回車:s = s.replace('\n','');
//其他照做。
注:\n 回車(\u000a)
\t 水平製表符(\u0009)
\s 空格(\u0008)
\r 換行(\u000d)*/
}