`
1717T.java.com
  • 浏览: 19079 次
  • 性别: Icon_minigender_1
  • 来自: 成都
最近访客 更多访客>>
文章分类
社区版块
存档分类

过滤特殊字符串

阅读更多

  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.           }    

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics