Java – string contains at least one number
•
Java
I try to see if the string contains at least one number, either lowercase or uppercase
I wrote this:
int combinations = 0; string pass = "!!!AAabas1"; if (pass.matches("[0-9]")) { combinations = combinations + 10; } if (pass.matches("[a-z]")) { combinations =combinations + 26; } if (pass.matches("[A-Z]")) { combinations =combinations + 26; }
But I don't understand why I can't get the combination to 36 They just kept it at 0 What did I do wrong?
Solution
You can use pattern. I think the "matches" method looks up the entire string to match the regular expression
Try the next code:
int combinations = 0; String pass = "!!AAabas1"; if (Pattern.compile("[0-9]").matcher(pass).find()) { combinations = combinations + 10; } if (Pattern.compile("[a-z]").matcher(pass).find()) { combinations = combinations + 26; } if (Pattern.compile("[A-Z]").matcher(pass).find()) { combinations = combinations + 26; }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码