Java - check string content

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?

+3


source to share


3 answers


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.

+11


source


myString.matches("[^a-zA-Z0-9_-]*");

      



+1


source


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());

      

0


source







All Articles