How would I find out if a string starts with a lowercase letter by using an 'if' statement?
The islower() method returns True if all alphabets in a string are lowercase alphabets. If the string contains at least one uppercase alphabet, it returns False.
To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!
To check whether a character is in Lowercase or not in Java, use the Character. isLowerCase() method.
If you want to cover more than a-z, you can use something like:
var first = string.charAt(0);
if (first === first.toLowerCase() && first !== first.toUpperCase())
{
// first character is a lowercase letter
}
Both checks are needed because there are characters (such as numbers) which are neither uppercase or lowercase. For example:
"1" === "1".toLowerCase() //=> true
"1" === "1".toLowerCase() && "1" !== "1".toUpperCase() //=> true && false => false
"é" === "é".toLowerCase() && "é" !== "é".toUpperCase() //=> true && true => true
seems like if a character is not equal to it's upper case state it is lower case.
var first = string.charAt(0);
if(first!=first.toUpperCase()){
first character is lower case
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With