2 回答
TA贡献1777条经验 获得超3个赞
尝试这个
public class StackOverflow {
static String blackListInput = "*[]#$";
static char c[] = {'*','[',']','#','$'};
public static boolean isBlackList(String val) {
if("".equals(val) || val == null) {
return false;
}
if(val.contains(Character.toString(c[0]))) {
return true;
}
if(val.contains(Character.toString(c[1]))) {
return true;
}
if(val.contains(Character.toString(c[2]))) {
return true;
}
if(val.contains(Character.toString(c[3]))) {
return true;
}
if(val.contains(Character.toString(c[4]))) {
return true;
}
return false;
}
public static boolean cleanseInput(String val) {
if(isBlackList(val))
return true;
return false;
}
public static void main(String[] args) {
boolean b = cleanseInput("*");
System.out.println(b);
}
}
TA贡献1818条经验 获得超7个赞
如果你想使用正则表达式模式
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexPattern {
public static boolean isBlackList(String value) {
String regex = "[a-zA-Z0-9\\-#\\*\\[\\]\\$\\s]{0,30}";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(value);
boolean b = m.matches();
return b;
}
public static boolean cleanseInput(String val) {
if(isBlackList(val))
return true;
return false;
}
public static void main(String[] args) {
boolean b = cleanseInput("fsa$assdhsahdfjfjkf");
System.out.println(b);
}
}
添加回答
举报