user2079954
user2079954

Reputation: 479

dash delimiters in java are changing the output

I am using the following below java code to convert the numerics to arabic

    String str = "1234-5678-9101";

    char[] chars = {'٠','١','٢','٣','٤','٥','٦','٧','٨','٩'};
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < str.length(); ++i) {
        if (Character.isDigit(str.charAt(i))) {
            builder.append(Chars[(int)(str.charAt(i))-48]);
        } else {
            builder.append(str.charAt(i));
        }
    }

the expected output is ٩١٠١-٥٦٧٨-١٢٣٤ but the result is ١٢٣٤-٥٦٧٨-٩١٠١ (reverse direction)

Upvotes: 1

Views: 311

Answers (2)

user2079954
user2079954

Reputation: 479

Thanks to everyone, this issue was resolved using the below code:

String str = "1234-5678-9101";

char[] chars = {'٠','١','٢','٣','٤','٥','٦','٧','٨','٩'};
StringBuilder builder = new StringBuilder();

for (int i = 0; i < str.length(); ++i) {
    if (Character.isDigit(str.charAt(i))) {
        builder.append(Chars[(int)(str.charAt(i))-48]);
    } else {
        builder.append("\u202A");
        builder.append(str.charAt(i));
    }
}

Upvotes: 1

Cebence
Cebence

Reputation: 2416

The code works, that's obvious. But it looks to me that the dash character (-) is breaking the rules of Arabic writing from right to left - it seems that each group of numbers is correct but the overall order is not.

expected: ٩١٠١-٥٦٧٨-١٢٣٤
actual: ١٢٣٤-٥٦٧٨-٩١٠١

I can't help much more than this because that's all I know on Arabic alphabet. Hope this helps.

Upvotes: 0

Related Questions