Check String whether it contains only Latin characters?

前端 未结 6 1943
感动是毒
感动是毒 2021-01-01 21:36

Greetings,

I am developing GWT application where user can enter his details in Japanese. But the \'userid\' and \'password\' should only contain English characters(L

6条回答
  •  天命终不由人
    2021-01-01 21:46

    For something this simple, I'd use a regular expression.

    private static final Pattern p = Pattern.compile("\\p{Alpha}+");
    
    static boolean isValid(String input) {
      Matcher m = p.matcher(input);
      return m.matches();
    }
    

    There are other pre-defined classes like \w that might work better.

提交回复
热议问题