Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check a word is already all uppercase?

Tags:

ruby

I want to be able to check if a word is already all uppercase. And it might also include numbers.

Example:

GO234 => yes
Go234 => no
like image 430
Jacob Avatar asked Dec 16 '11 03:12

Jacob


People also ask

How do I check if a string is all uppercase?

To check if a string is all uppercase, use the toUppercase() method to convert the string to uppercase and compare it to itself. If the comparison returns true , then the string is all uppercase.

How do you know if a character is uppercase?

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

How do I check lowercase?

isLowerCase() method can be used to determine if a letter is a lowercase letter. This method takes a char as argument and return a boolean value. If it returns true it means the character is in lowercase form. And if it returns false it means the character is not in lowercase form.

Which expression can be evaluated to determine whether a character ch is an uppercase alphabet?

The isupper() function checks if ch is in uppercase as classified by the current C locale. By default, the characters from A to Z (ascii value 65 to 90) are uppercase characters.


2 Answers

You can compare the string with the same string but in uppercase:

'go234' == 'go234'.upcase  #=> false 'GO234' == 'GO234'.upcase  #=> true 
like image 90
JCorcuera Avatar answered Oct 15 '22 10:10

JCorcuera


a = "Go234"
a.match(/\p{Lower}/) # => #<MatchData "o">

b = "GO234"
b.match(/\p{Lower}/) # => nil

c = "123"
c.match(/\p{Lower}/) # => nil

d = "µ"
d.match(/\p{Lower}/) # => #<MatchData "µ">

So when the match result is nil, it is in uppercase already, else something is in lowercase.

Thank you @mu is too short mentioned that we should use /\p{Lower}/ instead to match non-English lower case letters.

like image 42
PeterWong Avatar answered Oct 15 '22 10:10

PeterWong