# Problem when removing accents from a String

**URL:** <https://www.ruby-forum.com/t/problem-when-removing-accents-from-a-string/165339>\
**Category:** Ruby\
**Created:** [April 12, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/problem-when-removing-accents-from-a-string/165339 "2009-04-12T19:06:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cesardiaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dfb087/32.png) [@cesardiaz](https://www.ruby-forum.com/u/cesardiaz)\
**Post date:** [April 12, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/problem-when-removing-accents-from-a-string/165339/1 "2009-04-12T19:06:01Z")

</div>

Hi,

I’ve spend several time trying to replace latin characters in strings.  
What I want is something like this:

Given the word “camiÃ³n”, I would like to get the following string:  
“camion”.

I’ve found several solutions in the Internet but they only work in the  
Rails console, and when I try to launch my Rails application on Mongrel  
(I am using Netbeans) nothing is working and from “camiÃ³n” I am getting  
“camin” instead of “camion”.

For example, I’ve tried with these snippets:

1.\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
def nice\_slug(str)

```
accents = {
  ['Ã¡','Ã ','Ã¢','Ã¤','Ã£'] => 'a',
  ['Ãƒ','Ã„','Ã‚','Ã€','Ã'] => 'A',
  ['Ã©','Ã¨','Ãª','Ã«'] => 'e',
  ['Ã‹','Ã‰','Ãˆ','ÃŠ'] => 'E',
  ['Ã­','Ã¬','Ã®','Ã¯'] => 'i',
  ['Ã','ÃŽ','ÃŒ','Ã'] => 'I',
  ['Ã³','Ã²','Ã´','Ã¶','Ãµ'] => 'o',
  ['Ã•','Ã–','Ã”','Ã’','Ã“'] => 'O',
  ['Ãº','Ã¹','Ã»','Ã¼'] => 'u',
  ['Ãš','Ã›','Ã™','Ãœ'] => 'U',
  ['Ã§'] => 'c', ['Ã‡'] => 'C',
  ['Ã±'] => 'n', ['Ã‘'] => 'N'
  }
accents.each do |ac,rep|
  ac.each do |s|
  str = str.gsub(s, rep)
  end
end
str = str.gsub(/[^a-zA-Z0-9]/,"")

str = str.gsub(/[]+/," ")

str = str.gsub(/ /,"-")

str = str.downcase

```

end

2.\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
“camiÃ³n”.parameterize

It throws the following error:

undefined method `normalize’ for "camiï¿½n:String

c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active\_support/inflector.rb:283:in  
`transliterate' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/inflector.rb:262:in`parameterize’  
c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active\_support/core\_ext/string/inflections.rb:106:in  
`parameterize’

3.\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
“camiÃ³n”.mb\_chars.decompose.scan(/[a-zA-Z0-9]/).join

It throws the following error:

undefined method `decompose’ for "camiï¿½n:String

I am using Ruby 1.8.6, Rails 2.2, Mongrel 1.1.5 and Windows XP.

Please, some help.

---

<div class="post-metadata">

**Author:** ![cesardiaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/dfb087/32.png) [@cesardiaz](https://www.ruby-forum.com/u/cesardiaz)\
**Post date:** [April 12, 2009, 7:58pm UTC](https://www.ruby-forum.com/t/problem-when-removing-accents-from-a-string/165339/2 "2009-04-12T19:58:39Z")

</div>

Dear César,

have you also tried iconv  
([http://www.ruby-doc.org/stdlib/libdoc/iconv/rdoc/classes/Iconv.html](http://www.ruby-doc.org/stdlib/libdoc/iconv/rdoc/classes/Iconv.html) )?  
A quick fix might be to use it to convert between different,  
incompatible  
encodings, ignoring the accents (which occur in utf-8 or iso-8859-1  
encoding, but not in the target ascii encoding).

I can’t try it out on Windows XP right now, but you can see some  
discussion and examples here:

[http://www.ruby-forum.com/topic/70827](http://www.ruby-forum.com/topic/70827)

Another option might be to use the HTML entities gem.  
It gives you the names of the accented character, and you can then  
remove the accents in a small routine using regexps,

require ‘rubygems’  
require ‘htmlentities’  
coder = HTMLEntities.new  
string = “\<élan\>”  
res=coder.encode(string, :named) # =\> “\<élan\>”

and replace the name of the accent with a regexp:

res.gsub(/&(.)(acute|grave|circ|tilde|cedil|ring|slash);/,‘\1’)

This works well for the western European and northern European  
languages,  
but it fails for some accents of middle Eastern European languages, such  
as the Polish ogoneks ([Ogonek - Wikipedia](http://en.wikipedia.org/wiki/Ogonek)) and the  
carons of Czech : [Caron - Wikipedia](http://en.wikipedia.org/wiki/Hacek)) …

Best regards,

Axel
