# Detect whether unicode string is Japanese

**URL:** <https://www.ruby-forum.com/t/detect-whether-unicode-string-is-japanese/149768>\
**Category:** Ruby\
**Created:** [September 29, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/detect-whether-unicode-string-is-japanese/149768 "2008-09-29T16:19:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bobm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9fc348/32.png) [@bobm](https://www.ruby-forum.com/u/bobm)\
**Post date:** [September 29, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/detect-whether-unicode-string-is-japanese/149768/1 "2008-09-29T16:19:44Z")

</div>

How can I get a tally of how many characters in a Unicode string are  
Japanese (hiragana, katakana, kanji)? When I unpack a string, each  
character comes out like \xE3\x81\x95, but I am trying to check if it’s  
in the range 3040-309F (Hiragana) and I don’t understand how to convert  
between the 3-byte representation and that range…

---

<div class="post-metadata">

**Author:** ![bobm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/9fc348/32.png) [@bobm](https://www.ruby-forum.com/u/bobm)\
**Post date:** [September 30, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/detect-whether-unicode-string-is-japanese/149768/2 "2008-09-30T19:17:04Z")

</div>

On Monday 29 September 2008 16:18:53 Bob Marley wrote:

> How can I get a tally of how many characters in a Unicode string are  
> Japanese (hiragana, katakana, kanji)? When I unpack a string, each  
> character comes out like \xE3\x81\x95, but I am trying to check if it’s  
> in the range 3040-309F (Hiragana) and I don’t understand how to convert  
> between the 3-byte representation and that range…

You may lookup the unicode mapping on google, but you will have to write  
new  
function for each possible encoding (UTF-8,UTF16LE…).

Or, with ruby 1.9, you can iterate string by characters (not bytes), and  
use .ord function to get the unicode position number:

mystr.each\_char do |ch|  
puts ch.ord  
end

Jan
