Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if a string starts with a lowercase letter

Tags:

javascript

How would I find out if a string starts with a lowercase letter by using an 'if' statement?

like image 773
TheStandardRGB Avatar asked Sep 28 '10 20:09

TheStandardRGB


People also ask

How do you check if a string has a lowercase letter?

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.

How do you check if a string has a lowercase letter in Javascript?

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!

How do you check if a character in a string is lowercase Java?

To check whether a character is in Lowercase or not in Java, use the Character. isLowerCase() method.


2 Answers

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
like image 134
Daniel Vandersluis Avatar answered Oct 12 '22 20:10

Daniel Vandersluis


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
}
like image 36
kennebec Avatar answered Oct 12 '22 19:10

kennebec