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

查看:37
本文介绍了如何使用 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天全站免登陆