本文介绍了将String转换为java中的另一个语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我需要将阿拉伯语/波斯语数字转换为英语相等(例如将2转换为2)
我该怎么做?
Hi
I need to convert Arabic/Persian Numbers to it's English equal (for example convert "۲" to "2")
How can I do this?
谢谢
推荐答案
我建议你有一个十位数的查找字符串并替换所有数字一次一个。
I suggest you have a ten digit lookup String and replace all the digits one at a time.
public static void main(String... args) {
System.out.println(arabicToDecimal("۴۲"));
}
private static final String arabic = "\u06f0\u06f1\u06f2\u06f3\u06f4\u06f5\u06f6\u06f7\u06f8\u06f9";
private static String arabicToDecimal(String number) {
char[] chars = new char[number.length()];
for(int i=0;i<number.length();i++) {
char ch = number.charAt(i);
if (ch >= 0x0660 && ch <= 0x0669)
ch -= 0x0660 - '0';
else if (ch >= 0x06f0 && ch <= 0x06F9)
ch -= 0x06f0 - '0';
chars[i] = ch;
}
return new String(chars);
}
打印
42
将字符串用作查找的原因是其他字符,例如。
-
,
将保留为是。事实上,十进制数字将保持不变。
The reason for using the strings as a lookup is that other characters such as .
-
,
would be left as is. In fact a decimal number would be unchanged.
这篇关于将String转换为java中的另一个语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!