如何使用Ruby将阿拉伯语/波斯语数字翻译成英文? [英] How to translate Arabic/Persian numbers to english using Ruby?

查看:296
本文介绍了如何使用Ruby将阿拉伯语/波斯语数字翻译成英文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将具有阿拉伯语/波斯语数字的一些字符串转换为英文?

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

喜欢我有:

str1 = "١۲١۲"
str2 = "12١۲"
str3 = "some string that contains persian digits like ١۲"



有没有任何函数将其编码为英文,如果字符串包含这样的数字,就像最终结果一样转换为:

Is there any function to encode it to english, and if the string contain such number to convert it like end results will be :

str1 = "1212"
str2 = "1212"
str3 = "some string that contains persian digits like 12"

谢谢

推荐答案

对于这些一个转换, tr -method非常方便快捷。它在 tr!中有一个突变的对应物

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"

这篇关于如何使用Ruby将阿拉伯语/波斯语数字翻译成英文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆