I need to perform the following check:
IF myString.contains()
ANY CHARACTERS OTHER THAN
letters a-z, A-Z, "_", "-", numbers 0-9
THEN .....
whats is the correct java syntax for such a check?
I need to perform the following check:
IF myString.contains()
ANY CHARACTERS OTHER THAN
letters a-z, A-Z, "_", "-", numbers 0-9
THEN .....
whats is the correct java syntax for such a check?
You could use a regular expression
Pattern badChar = Pattern.compile("[^A-Za-z0-9_-]");
if(badChar.matcher(myString).find()) {
// ...
}
This pattern will match any single character apart from letters, numbers, underscore and hyphen.
myString contains one then find would return true. If you want to allow non-Latin letters you could use "[^\p{L}0-9_-]" - \p{L} matches any character that is a "letter" according to the Unicode standard.myString.matches("[^a-zA-Z0-9_-]*");
* or + to that to allow for a string with more than one character.