import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 康小岱
* 检测密码的合法性
* 规则说明:
* 1.密码不能含有空格字符串
* 2.密码只能包括字母和数字
* */
public class e {
public static void main(String[] args) {
String password = "我";
if (true == isValid(password)) {
System.out.println("合法密码格式");
} else {
System.out.println("不合法密码格式");
}
}
public static boolean isValid(String password) {
if (password.length() > 0) {
//判断是否有空格字符串
for (int t = 0; t < password.length(); t++) {
String b = password.substring(t, t + 1);
if (b.equals(" ")) {
System.out.println("有空字符串");
return false;
}
}
//判断是否有汉字
int count = 0;
String regEx = "[\\u4e00-\\u9fa5]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(password);
while (m.find()) {
for (int i = 0; i <= m.groupCount(); i++) {
count = count + 1;
}
}
if(count>0){
System.out.println("有汉字");
return false;
}
//判断是否是字母和数字
int numberCounter = 0;
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (!Character.isLetterOrDigit(c)) {
return false;
}
if (Character.isDigit(c)) {
numberCounter++;
}
}
} else {
return false;
}
return true;
}
}
Java判断密码字符串String的合法性:检测密码的合法性
最新推荐文章于 2024-08-07 16:07:44 发布