What is the best way to capitalize the first character of a UTF8 string?
String#capitalize downcases the rest of the string which is unacceptable
since there can be capitals in the middle of the string. ("see New
York".capitalize => "See new york"). Just picking off the first byte and
capitalizing that is also not an option since the first character can be
a multibyte UTF-8 character.
The only thing I can think of so far is either using Ruby 1.9 or the the
Rails multibyte Chars library to split off the first character and then
capitalize that and join it back together.
Surely there must be a better way for such a common problem?
on 20.03.2008 15:15
on 12.08.2008 23:23
I am having the same problem. What's the best solution sans-migrating to a new version of Ruby?
on 12.08.2008 23:34
I ended up doing it like this:
Class ::String
def firstcap
self.gsub(/^(\w)/) { $1.chars.capitalize } # /^([a-z])/
end
end