# Remove non-ASCII characters in a string

**URL:** <https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224>\
**Category:** Ruby\
**Created:** [January 22, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224 "2006-01-22T19:42:31Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 22, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/1 "2006-01-22T19:42:31Z")

</div>

Hi,

i needed a method to convert a piece of text to plain ascii and  
replace all non-ascii chars with a placeholder. I could not find  
anything in the stdlib so I wrote one.

I’d love to hear your comments. (or pointers to existing libraries for  
this task)

-Levin

#!/usr/bin/ruby

require ‘iconv’

class String

# removes all characters which are not part of ascii

# and replaces them with +replacement+

# 

# +replacement+ is supposed to be the same encoding as +source+

# 

def asciify(replacement = “?”, target = “ASCII”, source = “UTF-8”)  
intermediate = “UCS-4”  
pack\_format = “N\*”  
i = Iconv.new(intermediate, source)

```
u16s = i.iconv(self)
repl = i.iconv(replacement).unpack(pack_format)

s = u16s.unpack(pack_format).collect { |codepoint|
  codepoint < 128 ? codepoint : repl
}.flatten.pack(pack_format)

return Iconv.new(target, intermediate).iconv(s)

```

end  
end

if **FILE** == $0  
require ‘test/unit’

class TestAsciify \< Test::Unit::TestCase  
def test\_asciify  
assert\_equal “IÃ±tÃ«rnÃ¢tiÃ´nÃ lizÃ¦tiÃ¸n”.asciify,  
“I?t?rn?ti?n?liz?ti?n”  
assert\_equal “MÃ¶torhead”.asciify("(removed)"), “M(removed)torhead”  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 22, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/2 "2006-01-22T23:22:13Z")

</div>

I have a need for something like this as well. But I need to  
replace the chars with something plain ascii besides a placeholder.  
Any ideas how to do that?

```
I ended up finding the escape codes for all the chars like "\322"

```

and friends so I could replace say curly quotes with standard quotes  
and stuff like that.

```
I will play with your code a bit and see if I can make it do what I

```

want. Thanks for sharing it though.

Cheers-  
-Ezra

On Jan 22, 2006, at 10:41 AM, Levin A. wrote:

> # +replacement+ is supposed to be the same encoding as +source+
> 
> ```
> codepoint < 128 ? codepoint : repl
> def test_asciify
> assert_equal "Iñtërnâtiônàlizætiøn".asciify, "I?t?rn?ti?n?liz? 
> 
> ```
> 
> ti?n"  
> assert\_equal “Mötorhead”.asciify(“(removed)”), “M(removed)  
> torhead”  
> end  
> end  
> end

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
[http://yakimaherald.com](http://yakimaherald.com)  
removed\_email\_address@domain.invalid  
blog: [http://brainspl.at](http://brainspl.at)

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 22, 2006, 11:46pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/3 "2006-01-22T23:46:29Z")

</div>

What is considered an ascii char? i used the  
[http://www.lookuptables.com/](http://www.lookuptables.com/) chart from ! to ~ or 33 … 127

class String  
def remove\_nonascii(replacement)  
n=self.split(“”)  
self.slice!(0…self.size)  
n.each{|b|  
if b[0].to\_i\< 33 || b[0].to\_i\>127 then  
self.concat(replacement)  
else  
self.concat(b)  
end  
}  
self.to\_s  
end  
end  
require ‘test/unit’

class TestAsciify \< Test::Unit::TestCase  
def test\_asciify  
assert\_equal “Iñtërnâtiônàlizætiøn”.remove\_nonascii(“?”),  
“I?t?rn?ti?n?liz?ti?n”  
assert\_equal “Mötorhead”.remove\_nonascii(“(removed)”),  
“M(removed)torhead”  
end  
end

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 23, 2006, 12:13am UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/4 "2006-01-23T00:13:48Z")

</div>

On 1/22/06, ruby talk [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is considered an ascii char? i used the  
> [http://www.lookuptables.com/](http://www.lookuptables.com/) chart from ! to ~ or 33 … 127

I use everything \<127 because I want to preserve tabs and linebreaks

> def remove\_nonascii(replacement)

This does not work if the source text is UTF-8 encoded. On my machine:

str = “Ã¶” #=\> “\303\266”  
str.remove\_nonascii #=\> “??”

-Levin

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 24, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/5 "2006-01-24T17:37:27Z")

</div>

Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My version might look like this:
> 
> NON\_ASCII = /[\x80-\xff]/  
> “Iñtërnâtiônàlizætiøn”.gsub(NON\_ASCII, “?”) #=\> “I?t?rn?ti?n?liz?ti?n”

i’d like not to remove no-ascii chars but replace all accentuated chars  
(in an UTF-8 string) by them non-accentuated counterpart :

è =\> e  
ä =\> a  
ç =\> c

[…]

what is the best way to do that in Ruby?

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 24, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/6 "2006-01-24T17:31:19Z")

</div>

ruby talk wrote:

> What is considered an ascii char? i used the  
> [http://www.lookuptables.com/](http://www.lookuptables.com/) chart from ! to ~ or 33 … 127

Your range excludes ASCII character 32, space.  
" “.remove\_nonascii(”?") #=\> “?”

You also likely want to include characters like tabs and newlines, which  
are  
in the 0-31 control range.

Levin’s original version treats the original text as UTF-8. Is that part  
of  
the requirements?

My version might look like this:

NON\_ASCII = /[\x80-\xff]/  
“Iñtërnâtiônàlizætiøn”.gsub(NON\_ASCII, “?”) #=\> “I?t?rn?ti?n?liz?ti?n”

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 24, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/7 "2006-01-24T17:40:36Z")

</div>

“Une bévue” wrote:

> è =\> e  
> ä =\> a  
> ç =\> c
> 
> […]
> 
> what is the best way to do that in Ruby?

Try the code below, translated from

> **[Display UCS-2 and UTF-8 codes - About](http://stuffofinterest.com/misc/utf8-about.html)**
>
> Discussion of comparison of Unicode UCS-2 and UTF-8 character encodings

There may be a potential problem matching over character boundaries, but  
I  
think UTF-8’s unique starting bytes avoid the issue. So this should  
work.  
For long strings, it could be slow. If I wanted speed, I’d probably do  
the  
same thing in C and make it an extension.

Cheers,  
Dave

class String

# Translate accented utf8 characters over to non-accented

def utf8\_trans\_unaccent  
tranmap = {  
“\xC3\x80” =\> “A”, “\xC3\x81” =\> “A”, “\xC3\x82” =\> “A”, “\xC3\x83”  
=\>  
“A”,  
“\xC3\x84” =\> “A”, “\xC3\x85” =\> “A”, “\xC3\x86” =\> “AE”,“\xC3\x87”  
=\>  
“C”,  
“\xC3\x88” =\> “E”, “\xC3\x89” =\> “E”, “\xC3\x8A” =\> “E”, “\xC3\x8B”  
=\>  
“E”,  
“\xC3\x8C” =\> “I”, “\xC3\x8D” =\> “I”, “\xC3\x8E” =\> “I”, “\xC3\x8F”  
=\>  
“I”,  
“\xC3\x90” =\> “D”, “\xC3\x91” =\> “N”, “\xC3\x92” =\> “O”, “\xC3\x93”  
=\>  
“O”,  
“\xC3\x94” =\> “O”, “\xC3\x95” =\> “O”, “\xC3\x96” =\> “O”, “\xC3\x98”  
=\>  
“O”,  
“\xC3\x99” =\> “U”, “\xC3\x9A” =\> “U”, “\xC3\x9B” =\> “U”, “\xC3\x9C”  
=\>  
“U”,  
“\xC3\x9D” =\> “Y”, “\xC3\x9E” =\> “P”, “\xC3\x9F” =\> “ss”,  
“\xC3\xA0” =\> “a”, “\xC3\xA1” =\> “a”, “\xC3\xA2” =\> “a”, “\xC3\xA3”  
=\>  
“a”,  
“\xC3\xA4” =\> “a”, “\xC3\xA5” =\> “a”, “\xC3\xA6” =\> “ae”,“\xC3\xA7”  
=\>  
“c”,  
“\xC3\xA8” =\> “e”, “\xC3\xA9” =\> “e”, “\xC3\xAA” =\> “e”, “\xC3\xAB”  
=\>  
“e”,  
“\xC3\xAC” =\> “i”, “\xC3\xAD” =\> “i”, “\xC3\xAE” =\> “i”, “\xC3\xAF”  
=\>  
“i”,  
“\xC3\xB0” =\> “o”, “\xC3\xB1” =\> “n”, “\xC3\xB2” =\> “o”, “\xC3\xB3”  
=\>  
“o”,  
“\xC3\xB4” =\> “o”, “\xC3\xB5” =\> “o”, “\xC3\xB6” =\> “o”, “\xC3\xB8”  
=\>  
“o”,  
“\xC3\xB9” =\> “u”, “\xC3\xBA” =\> “u”, “\xC3\xBB” =\> “u”, “\xC3\xBC”  
=\>  
“u”,  
“\xC3\xBD” =\> “y”, “\xC3\xBE” =\> “p”, “\xC3\xBF” =\> “y”,  
“\xC4\x80” =\> “A”, “\xC4\x81” =\> “a”, “\xC4\x82” =\> “A”, “\xC4\x83”  
=\>  
“a”,  
“\xC4\x84” =\> “A”, “\xC4\x85” =\> “a”, “\xC4\x86” =\> “C”, “\xC4\x87”  
=\>  
“c”,  
“\xC4\x88” =\> “C”, “\xC4\x89” =\> “c”, “\xC4\x8A” =\> “C”, “\xC4\x8B”  
=\>  
“c”,  
“\xC4\x8C” =\> “C”, “\xC4\x8D” =\> “c”, “\xC4\x8E” =\> “D”, “\xC4\x8F”  
=\>  
“d”,  
“\xC4\x90” =\> “D”, “\xC4\x91” =\> “d”, “\xC4\x92” =\> “E”, “\xC4\x93”  
=\>  
“e”,  
“\xC4\x94” =\> “E”, “\xC4\x95” =\> “e”, “\xC4\x96” =\> “E”, “\xC4\x97”  
=\>  
“e”,  
“\xC4\x98” =\> “E”, “\xC4\x99” =\> “e”, “\xC4\x9A” =\> “E”, “\xC4\x9B”  
=\>  
“e”,  
“\xC4\x9C” =\> “G”, “\xC4\x9D” =\> “g”, “\xC4\x9E” =\> “G”, “\xC4\x9F”  
=\>  
“g”,  
“\xC4\xA0” =\> “G”, “\xC4\xA1” =\> “g”, “\xC4\xA2” =\> “G”, “\xC4\xA3”  
=\>  
“g”,  
“\xC4\xA4” =\> “H”, “\xC4\xA5” =\> “h”, “\xC4\xA6” =\> “H”, “\xC4\xA7”  
=\>  
“h”,  
“\xC4\xA8” =\> “I”, “\xC4\xA9” =\> “i”, “\xC4\xAA” =\> “I”, “\xC4\xAB”  
=\>  
“i”,  
“\xC4\xAC” =\> “I”, “\xC4\xAD” =\> “i”, “\xC4\xAE” =\> “I”, “\xC4\xAF”  
=\>  
“i”,  
“\xC4\xB0” =\> “I”, “\xC4\xB1” =\> “i”, “\xC4\xB2” =\> “IJ”,“\xC4\xB3”  
=\>  
“ij”,  
“\xC4\xB4” =\> “J”, “\xC4\xB5” =\> “j”, “\xC4\xB6” =\> “K”, “\xC4\xB7”  
=\>  
“k”,  
“\xC4\xB8” =\> “k”, “\xC4\xB9” =\> “L”, “\xC4\xBA” =\> “l”, “\xC4\xBB”  
=\>  
“L”,  
“\xC4\xBC” =\> “l”, “\xC4\xBD” =\> “L”, “\xC4\xBE” =\> “l”, “\xC4\xBF”  
=\>  
“L”,  
“\xC5\x80” =\> “l”, “\xC5\x81” =\> “L”, “\xC5\x82” =\> “l”, “\xC5\x83”  
=\>  
“N”,  
“\xC5\x84” =\> “n”, “\xC5\x85” =\> “N”, “\xC5\x86” =\> “n”, “\xC5\x87”  
=\>  
“N”,  
“\xC5\x88” =\> “n”, “\xC5\x89” =\> “n”, “\xC5\x8A” =\> “N”, “\xC5\x8B”  
=\>  
“n”,  
“\xC5\x8C” =\> “O”, “\xC5\x8D” =\> “o”, “\xC5\x8E” =\> “O”, “\xC5\x8F”  
=\>  
“o”,  
“\xC5\x90” =\> “O”, “\xC5\x91” =\> “o”, “\xC5\x92” =\> “CE”,“\xC5\x93”  
=\>  
“ce”,  
“\xC5\x94” =\> “R”, “\xC5\x95” =\> “r”, “\xC5\x96” =\> “R”, “\xC5\x97”  
=\>  
“r”,  
“\xC5\x98” =\> “R”, “\xC5\x99” =\> “r”, “\xC5\x9A” =\> “S”, “\xC5\x9B”  
=\>  
“s”,  
“\xC5\x9C” =\> “S”, “\xC5\x9D” =\> “s”, “\xC5\x9E” =\> “S”, “\xC5\x9F”  
=\>  
“s”,  
“\xC5\xA0” =\> “S”, “\xC5\xA1” =\> “s”, “\xC5\xA2” =\> “T”, “\xC5\xA3”  
=\>  
“t”,  
“\xC5\xA4” =\> “T”, “\xC5\xA5” =\> “t”, “\xC5\xA6” =\> “T”, “\xC5\xA7”  
=\>  
“t”,  
“\xC5\xA8” =\> “U”, “\xC5\xA9” =\> “u”, “\xC5\xAA” =\> “U”, “\xC5\xAB”  
=\>  
“u”,  
“\xC5\xAC” =\> “U”, “\xC5\xAD” =\> “u”, “\xC5\xAE” =\> “U”, “\xC5\xAF”  
=\>  
“u”,  
“\xC5\xB0” =\> “U”, “\xC5\xB1” =\> “u”, “\xC5\xB2” =\> “U”, “\xC5\xB3”  
=\>  
“u”,  
“\xC5\xB4” =\> “W”, “\xC5\xB5” =\> “w”, “\xC5\xB6” =\> “Y”, “\xC5\xB7”  
=\>  
“y”,  
“\xC5\xB8” =\> “Y”, “\xC5\xB9” =\> “Z”, “\xC5\xBA” =\> “z”, “\xC5\xBB”  
=\>  
“Z”,  
“\xC5\xBC” =\> “z”, “\xC5\xBD” =\> “Z”, “\xC5\xBE” =\> “z”, “\xC6\x8F”  
=\>  
“E”,  
“\xC6\xA0” =\> “O”, “\xC6\xA1” =\> “o”, “\xC6\xAF” =\> “U”, “\xC6\xB0”  
=\>  
“u”,  
“\xC7\x8D” =\> “A”, “\xC7\x8E” =\> “a”, “\xC7\x8F” =\> “I”,  
“\xC7\x90” =\> “i”, “\xC7\x91” =\> “O”, “\xC7\x92” =\> “o”, “\xC7\x93”  
=\>  
“U”,  
“\xC7\x94” =\> “u”, “\xC7\x95” =\> “U”, “\xC7\x96” =\> “u”, “\xC7\x97”  
=\>  
“U”,  
“\xC7\x98” =\> “u”, “\xC7\x99” =\> “U”, “\xC7\x9A” =\> “u”, “\xC7\x9B”  
=\>  
“U”,  
“\xC7\x9C” =\> “u”,  
“\xC7\xBA” =\> “A”, “\xC7\xBB” =\> “a”, “\xC7\xBC” =\> “AE”,“\xC7\xBD”  
=\>  
“ae”,  
“\xC7\xBE” =\> “O”, “\xC7\xBF” =\> “o”,  
“\xC9\x99” =\> “e”,

```
"\xC2\x82" => ",", # High code comma
"\xC2\x84" => ",,", # High code double comma
"\xC2\x85" => "...", # Tripple dot
"\xC2\x88" => "^", # High carat
"\xC2\x91" => "\x27", # Forward single quote
"\xC2\x92" => "\x27", # Reverse single quote
"\xC2\x93" => "\x22", # Forward double quote
"\xC2\x94" => "\x22", # Reverse double quote
"\xC2\x96" => "-", # High hyphen
"\xC2\x97" => "--", # Double hyphen
"\xC2\xA6" => "|", # Split vertical bar
"\xC2\xAB" => "<<", # Double less than
"\xC2\xBB" => ">>", # Double greater than
"\xC2\xBC" => "1/4", # one quarter
"\xC2\xBD" => "1/2", # one half
"\xC2\xBE" => "3/4", # three quarters

"\xCA\xBF" => "\x27", # c-single quote
"\xCC\xA8" => "", # modifier - under curve
"\xCC\xB1" => "" # modifier - under line

```

}

tranmap.inject(self) do |str, (utf8, asc)|  
p [utf8, asc]  
str.gsub(utf8, asc)  
end  
end  
end

“Iñtërnâtiônàlizætiøn”.utf8\_trans\_unaccent #=\> “Internationalizaetion”

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 24, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/8 "2006-01-24T17:40:37Z")

</div>

Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ry the code below, translated from  
> [Display UCS-2 and UTF-8 codes - About](http://stuffofinterest.com/misc/utf8-about.html)
> 
> There may be a potential problem matching over character boundaries, but I  
> think UTF-8’s unique starting bytes avoid the issue. So this should work.  
> For long strings, it could be slow. If I wanted speed, I’d probably do the  
> same thing in C and make it an extension.

thanks a lot this works great even with ligatures, i don’t need speed  
because i’ll use that only for file names…

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 24, 2006, 5:43pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/9 "2006-01-24T17:43:41Z")

</div>

Ç?Ä?á¸?á»?Ã±Å¥Å?, you say? What about these (incomplete list, and w/o  
ligatures) :))))

a, 69,  
AaÂªÃ?ÃÃ?Ã?Ã?Ã?Ã Ã¡Ã¢Ã£Ã¤Ã¥Ä?ÄÄ?Ä?Ä?Ä?ÇÇ?Ç?Ç?Ç Ç¡ÇºÇ»È?ÈÈ?È?È¦È§á´¬áµ?á¸?á¸áº áº¡áº¢áº£áº¤áº¥áº¦áº§áº¨áº©áºªáº«áº¬áº­áº®áº¯áº°áº±áº²áº³áº´áºµáº¶áº·â?Ã?â?¶â?ï¼¡ï½  
b, 15, Bbá´®áµ?á¸?á¸?á¸?á¸?á¸?á¸?â?¬â?·â??ï¼¢ï½?  
c, 23, CcÃ?Ã§Ä?Ä?Ä?Ä?Ä?Ä?Ä?Äá¶?á¸?á¸?â??â?­â?­â?½â?¸â??ï¼£ï½?  
d, 29,  
DdÃÃ°Ä?ÄÄÄ?á´°áµ?á¶?á¸?á¸?á¸?á¸á¸?á¸á¸á¸?á¸?á¸?â??â??â?®â?¾â?¹â??ï¼¤ï½?  
e, 62,  
EeÃ?Ã?Ã?Ã?Ã¨Ã©ÃªÃ«Ä?Ä?Ä?Ä?Ä?Ä?Ä?Ä?Ä?Ä?È?È?È?È?È¨È©á´±áµ?á¸?á¸?á¸?á¸?á¸?á¸?á¸?á¸?á¸?á¸áº¸áº¹áººáº»áº¼áº½áº¾áº¿á»?á»á»?á»?á»?á»?á»?á»?â??â?¯â?°â??â?ºâ??ï¼¥ï½?  
f, 10, Ffá¶ á¸?á¸?â?±â?»â??ï¼¦ï½?  
g, 23, GgÄ?ÄÄ?Ä?Ä Ä¡Ä¢Ä£Ç¦Ç§Ç´Çµá´³áµá¸ á¸¡â??â?¼â??ï¼§ï½?  
h, 30,  
HhÄ¤Ä¥Ä¦Ä§È?È?Ê°á´´á¸¢á¸£á¸¤á¸¥á¸¦á¸§á¸¨á¸©á¸ªá¸«áº?â??â??â?â??â?â?½â??ï¼¨ï½?  
i, 46,  
IiÃ?ÃÃ?ÃÃ¬Ã­Ã®Ã¯Ä¨Ä©ÄªÄ«Ä¬Ä­Ä®Ä¯Ä°ÇÇÈ?È?È?È?á´µáµ¢á¸¬á¸­á¸®á¸¯á»?á»?á»?á»?â±â?â??â?¹â??â? â?°â?¾â??ï¼©ï½?  
j, 12, JjÄ´ÄµÇ°Ê²á´¶â??â?¿â??ï¼ªï½?  
k, 19, KkÄ¶Ä·Ç¨Ç©á´·áµá¸°á¸±á¸²á¸³á¸´á¸µKâ??â??ï¼«ï½?  
l, 28,  
LlÄ¹ÄºÄ»Ä¼Ä½Ä¾ÅÅ?Ë¡á´¸á¸¶á¸·á¸¸á¸¹á¸ºá¸»á¸¼á¸½â??â??â?¬â?¼â?â??ï¼¬ï½?  
m, 17, Mmá´¹áµá¸¾á¸¿á¹?á¹á¹?á¹?â?³â?¯â?¿â??â??ï¼­ï½  
n, 27,  
NnÃ?Ã±Å?Å?Å?Å?Å?Å?Ç¸Ç¹á´ºá¹?á¹?á¹?á¹?á¹?á¹?á¹?á¹?â¿â??â??â?ï¼®ï½?  
o, 83,  
OoÂºÃ?Ã?Ã?Ã?Ã?Ã?Ã²Ã³Ã´ÃµÃ¶Ã¸Å?ÅÅ?ÅÅÅ?Æ Æ¡Ç?Ç?ÇªÇ«Ç¬Ç­Ç¾Ç¿È?ÈÈ?ÈÈªÈ«È¬È­È®È¯È°È±á´¼áµ?á¹?á¹á¹?á¹á¹á¹?á¹?á¹?á»?á»á»?á»á»á»?á»?á»?á»?á»?á»?á»?á»?á»?á»?á»?á»?á»á»?á»?á» á»¡á»¢á»£â??â?´â??â??ï¼¯ï½  
p, 13, Ppá´¾áµ?á¹?á¹?á¹?á¹?â??â??â??ï¼°ï½  
q, 7, Qqâ??â??â? ï¼±ï½?  
r, 30,  
RrÅ?Å?Å?Å?Å?Å?ÈÈ?È?È?Ê³á´¿áµ£á¹?á¹?á¹?á¹?á¹?á¹á¹?á¹?â??â??â?â??â?¡ï¼²ï½?  
s, 29,  
SsÅ?Å?Å?ÅÅ?Å?Å Å¡Å¿È?È?Ë¢á¹ á¹¡á¹¢á¹£á¹¤á¹¥á¹¦á¹§á¹¨á¹©áº?â??â?¢ï¼³ï½?  
t, 23, TtÅ¢Å£Å¤Å¥È?È?áµ?áµ?á¹ªá¹«á¹¬á¹­á¹®á¹¯á¹°á¹±áº?â??â?£ï¼´ï½?  
u, 69,  
UuÃ?Ã?Ã?Ã?Ã¹ÃºÃ»Ã¼Å¨Å©ÅªÅ«Å¬Å­Å®Å¯Å°Å±Å²Å³Æ¯Æ°Ç?Ç?Ç?Ç?Ç?Ç?Ç?Ç?Ç?Ç?È?È?È?È?áµáµ?áµ¤á¹²á¹³á¹´á¹µá¹¶á¹·á¹¸á¹¹á¹ºá¹»á»¤á»¥á»¦á»§á»¨á»©á»ªá»«á»¬á»­á»®á»¯á»°á»±â??â?¤ï¼µï½?  
v, 14, Vváµ?áµ¥á¹¼á¹½á¹¾á¹¿â?¤â?´â??â?¥ï¼¶ï½?  
w, 21, WwÅ´ÅµÊ·áµ?áº?áºáº?áº?áº?áº?áº?áº?áº?áº?áº?â??â?¦ï¼·ï½?  
x, 14, XxË£áº?áº?áº?áºâ??â?©â?¹â?â?§ï¼¸ï½?  
y, 26,  
YyÃÃ½Ã¿Å¶Å·Å¸È²È³Ê¸áº?áºáº?á»²á»³á»´á»µá»¶á»·á»¸á»¹â??â?¨ï¼¹ï½?  
z, 21, ZzÅ¹ÅºÅ»Å¼Å½Å¾á¶»áºáº?áº?áº?áº?áº?â?¤â?¨â?â?©ï¼ºï½?

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 24, 2006, 5:46pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/10 "2006-01-24T17:46:45Z")

</div>

Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> tranmap.inject(self) do |str, (utf8, asc)|  
> p [utf8, asc]  
> str.gsub(utf8, asc)  
> end  
> end  
> end
> 
> “Iñtërnâtiônàlizætiøn”.utf8\_trans\_unaccent #=\> “Internationalizaetion”

# one time preprocessing

INTL, ASC = “”, “”  
tranmap.each {|k,v|  
INTL \<\< k  
ASC \<\< v  
}

# quicker than repeated gsubs:

str.tr(INTL, ASC)

martin

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 24, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/11 "2006-01-24T17:49:47Z")

</div>

Martin DeMello wrote:

> # one time preprocessing
> 
> INTL, ASC = “”, “”  
> tranmap.each {|k,v|  
> INTL \<\< k  
> ASC \<\< v  
> }
> 
> # quicker than repeated gsubs:
> 
> str.tr(INTL, ASC)

Except that won’t work, because tr only matches bytes, not multi-byte  
characters.

(It might work after applying one of the Unicode string extensions that  
have  
been floating around recently. But not in standard Ruby.)

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 24, 2006, 5:55pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/12 "2006-01-24T17:55:53Z")

</div>

Dave B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Except that won’t work, because tr only matches bytes, not multi-byte  
> characters.
> 
> (It might work after applying one of the Unicode string extensions that have  
> been floating around recently. But not in standard Ruby.)

Oh - didn’t know that! Pretty sad. Thanks for the correction.

martin

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 24, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/13 "2006-01-24T18:38:56Z")

</div>

On 1/22/06, Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> I have a need for something like this as well. But I need to
> 
> ```
> 
> replace the chars with something plain ascii besides a placeholder.  
> Any ideas how to do that?

What I have now looks like this:

# basic usage

“schÃ¶n”.asciify #=\> “sch?n”

# with mapping

map = Asciify::Mapping.new(:default)  
“â??foo”".asciify(map) #=\> ‘“foo”’  
Asciify.new(map).convert(“schÃ¶n”) #=\> “schoen”  
Asciify.new(Asciify::HTMLEntities.new).convert(“schÃ¶n”) #=\>  
“schön”

Mapping.new(:default) reads the mappings from a YAML file, you can use  
Mapping.new(“file.yaml”) to load your own mappings or supply a Hash or  
lambda to Asciify.new.

I have put it on rubyforge. This is my very first piece of released  
code I’d very much like to hear your comments and criticism.

I’ll try to work in the posted mappings.

Regards,  
Levin

---

<div class="post-metadata">

**Author:** ![Levin\_A](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/eada6e/32.png) [@Levin\_A](https://www.ruby-forum.com/u/Levin_A)\
**Post date:** [January 24, 2006, 7:39pm UTC](https://www.ruby-forum.com/t/remove-non-ascii-characters-in-a-string/64224/14 "2006-01-24T19:39:48Z")

</div>

removed\_email\_address@domain.invalid (Une bévue) writes:

> è =\> e  
> ä =\> a  
> ç =\> c
> 
> […]
> 
> what is the best way to do that in Ruby?

How about this:

require ‘iconv’  
puts Iconv.open(“ASCII//TRANSLIT”,  
“ISO-8859-1”).iconv(“Iñtërnâtiônàlizætiøn”)  
#=\> I~nt"ern^ati^on`alizaetion
