I have the following characters that I would like to be considered "illegal":
~
, #
, @
, *
, +
, %
, {
, }
, <
, >
, [
, ]
, |
, “
, ”
,
, _
, ^
I'd like to write a method that inspects a string and determines (true
/false
) if that string contains these illegals:
public boolean containsIllegals(String toExamine) {
return toExamine.matches("^.*[~#@*+%{}<>[]|"\_^].*$");
}
However, a simple matches(...)
check isn't feasible for this. I need the method to scan every character in the string and make sure it's not one of these characters. Of course, I could do something horrible like:
public boolean containsIllegals(String toExamine) {
for(int i = 0; i < toExamine.length(); i++) {
char c = toExamine.charAt(i);
if(c == '~')
return true;
else if(c == '#')
return true;
// etc...
}
}
Is there a more elegant/efficient way of accomplishing this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…