需求:输入密码必须为字母、数字或特殊字符
方法一:
for (int i = 0; i < pwdTv.length(); i++) { int temp = pwdTv.charAt(i); if (temp < 33 || temp > 126) { showToastShort("请按照规则设置密码"); return false; }
方法二:
String regEx = "[\x21-\x7E]"; Pattern pattern = Pattern.compile(regEx); for (int i = 0; i < pwdTv.length(); i++) { String temp = String.valueOf(pwdTv.charAt(i)); Matcher matcher = pattern.matcher(temp); if (!matcher.matches()) { showToastShort("请按照规则设置密码"); return false; } }
方法一是将每个字符转为ASCII码,方法二是将每个字符匹配正则