想把字符串中“\/:*?"<>|”这些字符置换掉
使用正则式能否实现?
哪位大虾能否说明一下,如何实现。要置换的字符可以是变化的。谢谢先

解决方案 »

  1.   

    http://java.sun.com/docs/books/tutorial/essential/regex/test_harness.html
      

  2.   

    例如String str = "hello, world ? ";str = str.replaceAll("[?]", ",hello");

    System.out.println(str);
      

  3.   

    str.replaceAll("[\\\/:\\*\\?<>|],"hello");
      

  4.   

    如果要替换 [ 和 ],可以之前加上双反斜杠例如 str = str.replaceAll("\\[","!");
      

  5.   

    谢谢大家
    高定
    String str1 = "abc\\";
    Pattern p = Pattern.compile("[\\\\/:\\*\\?\"<>|]");
    Matcher m = p.matcher(str1);
    str1 = m.replaceAll("_");出力结果:abc_