Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

Convert String to another locale in java

Hi
I need to convert Arabic/Persian Numbers to it's English equal (for example convert "?" to "2")
How can I do this?

Thanks

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

prints

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...