How to know that string contains at least 2 upper case letters? For example these are valid strings \"Lazy Cat\", \"NOt very lazy cat\". Working with Java 1.7.
This regex works.
string.matches(".*[A-Z].*[A-Z].*")
Try this:
string.matches("[A-Z]+.*[A-Z]+");
You have somany regex answers right now,
Go for it if you don't want to use **regex**
,
String someString = "abcDS";
int upperCount = 0;
for (char c : someString.toCharArray()) {
if (Character.isUpperCase(c)) {
upperFound++;
}
}
// upperFound here weather >2 or not
I'll now show you a full solution, I'll guide you.
If you don't want to use regex, you can simply loop on the String, chat by char and check whether it's an upper case:
for (int i=0;i<myStr.length();i++)
{
//as @sanbhat suggested, use Character#isUpperCase on each character..
}
Try with following regex:
"^(.*?[A-Z]){2,}.*$"
or
"^(.*?[A-Z]){2,}"