So far I have:
def capitalized?(str)
str[0] == str[0].upcase
end
THe problem wit this is that it returns true
for strings like "12345"
, "£$%^&"
and"9ball"
etc. I would like it to only return true if the first character is a capital letter.
You can use match?
to return true if the first character is a letter in the range of A
to Z
both uppercase or not:
def capitalized?(str)
str.match?(/\A[A-Z]/)
end
p capitalized?("12345") # false
p capitalized?("fooo") # false
p capitalized?("Fooo") # true
Also you can pass a regular expression to start_with?
:
p 'Foo'.start_with?(/[A-Z]/) # true
p 'foo'.start_with?(/[A-Z]/) # false
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