ITeye博客

过滤特殊字符串

1717T.java.com · 2010-12-15 22:18 · 编程语言 · Markdown

  1. // 过滤特殊字符
  2. public   static   String StringFilter(String   str)   throws   PatternSyntaxException   {
  3. // 只允许字母和数字
  4. // String   regEx  =  "[^a-zA-Z0-9]";
  5. // 清除掉所有特殊字符
  6. String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
  7. Pattern   p   =   Pattern.compile(regEx);
  8. Matcher   m   =   p.matcher(str);
  9. return   m.replaceAll("").trim();
  10. }
  11. @Test
  12. public    void   testStringFilter()   throws   PatternSyntaxException   {
  13. String   str   =   "*adCVs*34_a _09_b5*[/435^*&城池()^$$&*).{}+.|.)%%*(*.中国}34{45[]12.fd'*&999下面是中文的字符¥……{}【】。,;’“‘”?";
  14. System.out.println(str);
  15. System.out.println(StringFilter(str));
  16. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。