本文介绍了如何检查Java String是否包含至少一个大写字母,小写字母和数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道我可以通过一系列for循环遍历字符串的for循环来做到这一点,但那将是糟糕的编程。好吧,我的教授更喜欢我不这样做。我想使用正则表达式来执行此操作。
I know that I could do this with a series of for loops that iterate through the string but that would be terrible programming. Well, my professor prefers I don't do it this way. I'd like to use regular expressions to do this.
推荐答案
对于简单的字符串检查,单个扫描字符串是足够。由于Regex不会提供任何显着的好处,这里有一个简单的for循环来实现相同的目的:
For a simple string check, a single sweep through the string is enough. Since Regex will not offer any significant benefit, here is a simple for loop to achieve the same :
private static boolean checkString(String str) {
char ch;
boolean capitalFlag = false;
boolean lowerCaseFlag = false;
boolean numberFlag = false;
for(int i=0;i < str.length();i++) {
ch = str.charAt(i);
if( Character.isDigit(ch)) {
numberFlag = true;
}
else if (Character.isUpperCase(ch)) {
capitalFlag = true;
} else if (Character.isLowerCase(ch)) {
lowerCaseFlag = true;
}
if(numberFlag && capitalFlag && lowerCaseFlag)
return true;
}
return false;
}
试运行
System.out.println(checkString("aBCd1")); // output is true
System.out.println(checkString("abcd")); //output is false
我认为这应该有助于OP的特殊问题。
I think this should help OP's particular problem.
这篇关于如何检查Java String是否包含至少一个大写字母,小写字母和数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!