在java中将String转换为另一个语言环境 [英] Convert String to another locale in java
本文介绍了在java中将String转换为另一个语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要将阿拉伯/波斯数字转换为英文相等(例如将۲"转换为2")
我该怎么做?
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("۴۲"));
}
//used in Persian apps
private static final String extendedArabic = "u06f0u06f1u06f2u06f3u06f4u06f5u06f6u06f7u06f8u06f9";
//used in Arabic apps
private static final String arabic = "u0660u0661u0662u0663u0664u0665u0666u0667u0668u0669";
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.
这篇关于在java中将String转换为另一个语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文