Is there a simple way to convert the first letter of a string to downcase? String#capitalize
modifies the entire string. Sure, I can remove the first letter, downcase it and then append it in the beginning. But it seems kinda silly, is there a simpler way?
Note: I'll be dealing with only English words.
Edit: str[0] = str[0].downcase
doesn't work in JRuby 1.6 :(
Edit 2: In the end I settled on this:
word = "ABC"
first_capital_letter = word.match(/^([A-Z])/).to_s
if(first_capital_letter)
word = word.sub(first_capital_letter, first_capital_letter.downcase)
puts word
end
If you really don't want to downcase the first letter and re-append it you could do str.gsub(/^\w{1}/) { |m| m.downcase }
but that seems silly.
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