iturki iturki - 10 days ago 5
Android Question

Convert English numbers in a String to Arabic numbers

I have multiple strings with different formats that I need to display in some TextViews. I have no control over the length or the format of these strings. They could have numbers obly, text only, or a combination of both.

My goal is to display the strings in some TextViews, and in the process, convert any English numbers to Arabic numbers.

The following examples should clarify what I mean:

original string >> converted string
=====================================
test 123 >> test ١٢٣
test >> test
123 >> ١٢٣


I started by doing this:

txtView.setText(String.format(locale, "%d", fulltext));


but this assumes you have numbers only in your
fulltext
string.

Could you suggest how to work around this?

Answer

Lets say your fulltext string is this

String fulltext = "1a2b3c";

Replace the instances of all of the numbers with the corresponding Arabic numbers

fulltext = fulltext.replaceAll("1","١").replaceAll("2","٢").replaceAll("3","٣")

etc. Hopefully this is what you mean

Comments