Forum: Ruby String#downcase for accented letters in UTF-8?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Gioele B. (Guest)
on 2006-04-15 17:10
(Received via mailing list)
How can I convert to lowercase a string that contains accented
characters
encoded in UTF-8?
String#downcase does not work with accented letters.
Xavier N. (Guest)
on 2006-04-15 19:10
(Received via mailing list)
On Apr 15, 2006, at 15:08, Gioele B. wrote:

> How can I convert to lowercase a string that contains accented
> characters
> encoded in UTF-8?
> String#downcase does not work with accented letters.

I hand-coded that:

   # This library redefines String#tr so that it understands UTF-8.
   require 'jcode'

   def normalize_for_sorting(s)
     return nil if s.nil?
     norm = s.downcase
     norm.tr!('ÁÉÍÓÚ', 'aeiou')
     norm.tr!('ÀÈÌÒÙ', 'aeiou')
     norm.tr!('ÄËÏÖÜ', 'aeiou')
     norm.tr!('ÂÊÎÔÛ', 'aeiou')
     norm.tr!('áéíóú', 'aeiou')
     norm.tr!('àèìòù', 'aeiou')
     norm.tr!('äëïöü', 'aeiou')
     norm.tr!('âêîôû', 'aeiou')
     norm
   end

-- fxn
This topic is locked and can not be replied to.