I need to do the following check:
IF myString.contains ()
ANY CHARACTERS OTHER THAN
letters az, AZ, "_", "-", numbers 0-9
THEN .....
What is the correct java syntax for such a check?
You can use regex
Pattern badChar = Pattern.compile("[^A-Za-z0-9_-]"); if(badChar.matcher(myString).find()) { // ... }
This pattern will match any single character except letters, numbers, underscore, and hyphen.
myString.matches("[^a-zA-Z0-9_-]*");
This will do it
String str1 = "abc, pqr"; Pattern pattern = Pattern.compile ("^ [a-zA-Z0-9._, -] +");
Matcher matcher1 = pattern.matcher(str1); System.out.println(matcher1.matches());