Java Data Type How to - Check string to contain character that I didn't list








Question

We would like to know how to check string to contain character that I didn't list.

Answer

import java.util.regex.Pattern;
/*  w w  w  .j av  a 2 s.  c o  m*/
public class Main {
  private static final Pattern VALID_STR = Pattern.compile("(?i)[a-z0-9]+");

  public static boolean isValidStr(String s) {
    return VALID_STR.matcher(s).matches();
  }

  public static void main(String[] args) {
    System.out.println(isValidStr("abc12d")); // true
    System.out.println(isValidStr("abc ")); // false
  }
}