Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string contains uppercase java

Tags:

java

string

I am working on a program that lets a user input an alphanumeric key, and checks if it is a valid key against some criteria, one of these is whether or not the key contains an uppercase. TThis is what I have tried so far:

else if (key.contains("QWERTYUIOPASDFGHJKLZXCVBNM")){
    UI.println("Invalid, key contains a space, illegal character");
    }

This only detects the capital letters if they are written in this order. is there a command such as key.contains i should be using?

like image 226
louistcooper Avatar asked Feb 14 '26 16:02

louistcooper


1 Answers

With streams, you can to the following.

key.chars().anyMatch(Character::isUpperCase)

This is much lighter than regexes in terms of resources.

like image 182
Olivier Grégoire Avatar answered Feb 16 '26 05:02

Olivier Grégoire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!