How to translate Arabic/Persian numbers to english using Ruby?

前端 未结 2 469
傲寒
傲寒 2021-01-24 13:21

How can I convert some string that has Arabic/Persian number to English ?

Like if I have :

str1 = \"١۲١۲\"
str2 = \"12١۲\"
str3 = \"some string that con         


        
2条回答
  •  猫巷女王i
    2021-01-24 14:09

    Since this is not encoding but translation and assuming your problem limits to only those numbers (0-9), you could write a simple 1-to-1 mapping from arabic to english, something like this:

      arabic_to_english = {
      '٩' => 9,
      '٨' => 8,
      '٧' => 7,
      '٦' => 6,
      '٥' => 5,
      '٤' => 4,
      '٣' => 3,
      '٢' => 2,
      '١' => 1,
      '٠' => 0
    }
    

    And you just call the hash whenever needed:

       arabic_to_english['٧']
    

    Better if you extract this into a function of course.

提交回复
热议问题