天天看点

java中去除字符串中的空格,回车,换行符,制表符

 Java代码:

  1. public class StringUtil   
  2. {   
  3. public static void replaceBlank()   
  4. {   
  5.    Pattern p = Pattern.compile(“//s*|/t|/r|/n”);   
  6.    String str="I am a, I am Hello ok, /n new line ffdsa!";   
  7.    System.out.println("before:"+str);   
  8.    Matcher m = p.matcher(str);   
  9.    String after = m.replaceAll("");    
  10.    System.out.println("after:"+after);   
  11. }   
  12. public static void main(String[] args)    
  13. {   
  14.      replaceBlank();   
  15. }   
  16. }