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();
}
本文介绍了一种用于过滤字符串中特殊字符的方法,通过定义正则表达式并使用Pattern和Matcher类来实现。这种方法能够有效地清除字符串中的各种特殊符号,确保只保留字母和数字,适用于多种应用场景。
5685

被折叠的 条评论
为什么被折叠?



