How to capitalize UTF8 string without downcasing

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?

I am having the same problem. What’s the best solution sans-migrating
to a new version of Ruby?

I ended up doing it like this:

Class ::String
def firstcap
self.gsub(/^(\w)/) { $1.chars.capitalize } # /^([a-z])/
end
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs