Java替换各种特殊字符工具类
http://my.oschina.net/u/1245614/blog/511308
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
Java代码 public class StringFilterUtil { public static String stringFilter(String str) throws PatternSyntaxException { // 只允许字母和数字 // String regEx = "[^a-zA-Z0-9]"; // 清除掉所有特殊字符 String regEx = "[`~!@#$%^&*()+=|{}':;',//[//].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); return m.replaceAll("").trim(); } public static void main(String[] args) { String str = "*adCVs*34_a _09_b5*[/435^*&城池()^$$&*).{}+.|.)%%*(*.中国}34{45[]12.fd'*&999下面是中文的字符¥……{}【】。,;’“‘”?"; System.out.println(stringFilter(str)); } } |
