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));
}
}
Java替換各種特殊字符工具類 轉
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.