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

前端 未结 2 462
傲寒
傲寒 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条回答
  •  有刺的猬
    2021-01-24 14:23

    For these one on one transformations the tr-method is very convenient and fast. It has a mutating counterpart in tr!

    #encoding: utf-8
    
    str1 = "١۲١۲"
    str2 = "12١۲"
    str3 = "some string that contains persian digits like ١۲"
    
    [str1, str2, str3].each{|str| str.tr!('۰١۲۳۴۵۶۷۸۹','0123456789')}
    
    p str1, str2, str3
    #"1212"
    #"1212"
    #"some string that contains persian digits like 12"
    

提交回复
热议问题