How can I test if a letter in a string is uppercase or lowercase using JavaScript?
The answer by josh and maleki will return true on both upper and lower case if the character or the whole string is numeric. making the result a false result.
example using josh
var character = '5';
if (character == character.toUpperCase()) {
alert ('upper case true');
}
if (character == character.toLowerCase()){
alert ('lower case true');
}
another way is to test it first if it is numeric, else test it if upper or lower case
example
var strings = 'this iS a TeSt 523 Now!';
var i=0;
var character='';
while (i <= strings.length){
character = strings.charAt(i);
if (!isNaN(character * 1)){
alert('character is numeric');
}else{
if (character == character.toUpperCase()) {
alert ('upper case true');
}
if (character == character.toLowerCase()){
alert ('lower case true');
}
}
i++;
}
How to check if a char and/or string is uppercase or lowercase
This will work:
char hLow = 'h';
char hHigh = 'H';
char.IsLower(hLow); //returns true
char.IsUpper(hHigh); //returns true
This would work as well (but it's a rather old-school approach to it and doesn't work for accent letters):
(hLow >= 'a' && hLow <= 'z'); //returns true
(hHigh >= 'A' && hHigh <= 'Z'); //returns true
Also, if you want to check if all characters in a string
are uppercase/lowercase, you can do it like this:
string word = "UPPERCASE";
word.All(char.IsUpper); //returns true
word.All(char.IsLower); //returns false
Keep in mind that you need to have using System.Linq;
in the beginning of your code for this to work.
And if you want to check if a string
only contains letters, just use this (still using Linq
):
word.All(char.IsLetter); //returns true
There are more useful functions like this in Linq
which you can find yourself.
Check if a character is lowerCase or upperCase
var input = "The quick BroWn fOX jumpS Over tHe lazY DOg"
var inputArray = Array(input)
for character in inputArray {
var strLower = "[a-z]";
var strChar = NSString(format: "%c",character )
let strTest = NSPredicate(format:"SELF MATCHES %@", strLower );
if strTest .evaluateWithObject(strChar)
{
// lower character
}
else
{
// upper character
}
}
Check if all letter is uppercase or lowercase python, if there is one word in string is different print none
No need for loops here. Just simply use the functions isupper()
and islower()
in an if
-elif
block, and add an else
block to take care of mixed case (i.e., print out None
or NONE
), like so:
test_str = input()
if test_str.isupper():
print('UPPER')
elif test_str.islower():
print('LOWER')
else:
print(None) # or print('NONE')
Example input/output:
HELLO USER
UPPER
HELLO UsER
None
hello user
LOWER
hello User
None
Active reading: GeeksForGeeks
How to check if a string is all upper or lower case in Go?
You can of course compare the upper and lower cased strings in their entirety, or you can short-circuit the comparisons on the first failure, which would be more efficient when comparing long strings.
func IsUpper(s string) bool {
for _, r := range s {
if !unicode.IsUpper(r) && unicode.IsLetter(r) {
return false
}
}
return true
}
func IsLower(s string) bool {
for _, r := range s {
if !unicode.IsLower(r) && unicode.IsLetter(r) {
return false
}
}
return true
}
How do I detect if a character is uppercase or lowercase?
BOOL isUppercase = [[NSCharacterSet uppercaseLetterCharacterSet] characterIsMember:[s characterAtIndex:0]];
How to check if letter is upper or lower in PHP?
It is my opinion that making a preg_
call is the most direct, concise, and reliable call versus the other posted solutions here.
echo preg_match('~^\p{Lu}~u', $string) ? 'upper' : 'lower';
My pattern breakdown:
~ # starting pattern delimiter
^ #match from the start of the input string
\p{Lu} #match exactly one uppercase letter (unicode safe)
~ #ending pattern delimiter
u #enable unicode matching
Please take notice when ctype_
and < 'a'
fail with this battery of tests.
Code: (Demo)
$tests = ['âa', 'Bbbbb', 'Éé', 'iou', 'Δδ'];
foreach ($tests as $test) {
echo "\n{$test}:";
echo "\n\tPREG: " , preg_match('~^\p{Lu}~u', $test) ? 'upper' : 'lower';
echo "\n\tCTYPE: " , ctype_upper(mb_substr($test, 0, 1)) ? 'upper' : 'lower';
echo "\n\t< a: " , mb_substr($test, 0, 1) < 'a' ? 'upper' : 'lower';
$chr = mb_substr ($test, 0, 1, "UTF-8");
echo "\n\tMB: " , mb_strtoupper($chr, "UTF-8") == $chr ? 'upper' : 'lower';
}
Output:
âa:
PREG: lower
CTYPE: lower
< a: lower
MB: lower
Bbbbb:
PREG: upper
CTYPE: upper
< a: upper
MB: upper
Éé: <-- trouble
PREG: upper
CTYPE: lower <-- uh oh
< a: lower <-- uh oh
MB: upper
iou:
PREG: lower
CTYPE: lower
< a: lower
MB: lower
Δδ: <-- extended beyond question scope
PREG: upper <-- still holding up
CTYPE: lower
< a: lower
MB: upper <-- still holding up
If anyone needs to differentiate between uppercase letters, lowercase letters, and non-letters see this post.
It may be extending the scope of this question too far, but if your input characters are especially squirrelly (they might not exist in a category that Lu
can handle), you may want to check if the first character has case variants:
\p{L&} or \p{Cased_Letter}: a letter that exists in lowercase and uppercase variants (combination of Ll, Lu and Lt).
- Source: https://www.regular-expressions.info/unicode.html
To include Roman Numerals ("Number Letters") with SMALL
variants, you can add that extra range to the pattern if necessary.
https://www.fileformat.info/info/unicode/category/Nl/list.htm
Code: (Demo)
echo preg_match('~^[\p{Lu}\x{2160}-\x{216F}]~u', $test) ? 'upper' : 'not upper';
How to check if string has lowercase letter, uppercase letter and number
You have some problems with your checking logic. First of all you re-set your control variables before the while
condition could check them. Furthermore you used and
to test if all conditions are true, while or
is what you should be using. If you want to keep the basic structure of your code, the best way to go is to use only True
in your while statement and then break
the loop once all conditions are fulfilled. Of course, as shown in the other answers, there are much more compact ways to check that all conditions are fulfilled.
passwd = raw_input("enter your password: ")
upper = 0
lower = 0
number = 0
while True:
for x in passwd:
if x.isupper()==True:
print 'upper'
upper+=1
elif x.islower()==True:
print 'lower'
lower+=1
elif x.isalpha()==False:
print 'num'
number+=1
if len(passwd) < 7 or upper <= 0 or lower <= 0 or number <= 0:
print ('password not complex enough')
passwd = raw_input('please enter password again ')
upper = 0
lower = 0
number = 0
else:
break
print 'final password:', passwd
Output:
enter your password: pA
lower
upper
password not complex enough
please enter password again pA1ghlfx78
lower
upper
num
lower
lower
lower
lower
lower
num
num
final password: pA1ghlfx78
Related Topics
Adding Index List and Section Headers to Translated Tableview
How to Repeat Animation Forever in Swift (Huge_Valf)
Partial Application of 'Mutating' Method Is Not Allowed
Suppress Warnings from Dependencies with Swift Package Manager
Swift Build' on Terminal Throw 'Error: Root Manifest Not Found'
In Swift, How to Extend a Typealias
Get the Type of Anyobject Dynamically in Swift
How to Conform an Observableobject to the Codable Protocols
Is There a Github Markdown Language Identifier for Swift Code
Zoom to Fit Current Location and Annotation on Map
Compile Time Key Path Checking in Swift
Using Uilexicon to Implement Autocorrect in iOS 8 Keyboard Extension