如何检查 Java 字符串是否至少包含一个大写字母、小写字母和数字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40336374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:37:03  来源:igfitidea点击:

How do I check if a Java String contains at least one capital letter, lowercase letter, and number?

javastring

提问by Michael Drum

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.

我知道我可以使用一系列遍历字符串的 for 循环来做到这一点,但这将是糟糕的编程。好吧,我的教授更喜欢我不要这样做。我想使用正则表达式来做到这一点。

采纳答案by CyprUS

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 :

对于简单的字符串检查,单次扫描字符串就足够了。由于正则表达式不会提供任何显着的好处,这里是一个简单的 for 循环来实现相同的:

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;
}

Test run:

测试运行:

System.out.println(checkString("aBCd1")); // output is true
System.out.println(checkString("abcd")); //output is false

I think this should help OP's particular problem.

我认为这应该有助于解决 OP 的特殊问题。

回答by Ghost Developer

Try regular expression

试试正则表达式

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$

descriptions are as follow
(?=.*[a-z])  -- check lower case letter
(?=.*[A-Z]) -- check upper case letter
(?=.*\d) -- check one digit exists

回答by Saad

You can use Character class of java. So for lowercase, Character.isLowerCase(ch)Character.isUpperCase(ch). For this you have to iterate over the string. I know that bit is irrelevant to you, however you can use "\\d+"for numbers in a string.

您可以使用java的Character类。所以对于小写,Character.isLowerCase(ch)Character.isUpperCase(ch). 为此,您必须遍历 string。我知道该位与您无关,但是您可以将其"\\d+"用于字符串中的数字。

回答by sopheamak

you can use :

您可以使用 :

public static boolean isTextContainUpperCase(String text) {
       if (StringUtils.isEmpty(text)) {
              return false;
       }
       return !text.equals(text.toLowerCase());
}

回答by Yhondri

Example of @goshDeveloper's answer.

@goshDeveloper 的回答示例。

First create a Pattern variable with regular expression you want.

首先用你想要的正则表达式创建一个 Pattern 变量。

public final Pattern textPattern = Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$");

Second you can use it like this:

其次,您可以像这样使用它:

public boolean isTextValid(String textToCheck) {
return textPattern.matcher(textToCheck).matches();
}

回答by ?ukasz K

Using Java 9:

使用 Java 9:

public boolean isValid(String value) {
    return containsLowerCase(value) && 
           containsUpperCase(value) && 
           containsNumber(value);
}

private boolean containsLowerCase(String value) {
    return contains(value, i -> Character.isLetter(i) && Character.isLowerCase(i));
}

private boolean containsUpperCase(String value) {
    return contains(value, i -> Character.isLetter(i) && Character.isUpperCase(i));
}

private boolean containsNumber(String value) {
    return contains(value, Character::isDigit);
}

private boolean contains(String value, IntPredicate predicate) {
    return value.chars().anyMatch(predicate);
}

回答by Tanumay Saha

If you are looking to check whether your string contains uppercase letter or not, you can use below pattern:

如果您想检查您的字符串是否包含大写字母,您可以使用以下模式:

String s="abcDe_shdgwU";    //you can have any String here

if(s.matches(".*[A-Z].*"))
   System.out.println("The String contains Uppercase letter(s)");
else
  System.out.println("does not contain Uppercase letter(s)");

Hope that helps. Thank you. Regards, Tanumay Saha

希望有帮助。谢谢你。问候, 塔努迈萨哈