Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 282
iTurki Avatar asked Dec 01 '22 00:12

iTurki


1 Answers

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.replace("1","١").replace("2","٢").replace("3","٣");

etc. Hopefully this is what you mean

like image 142
Andrew Brooke Avatar answered Dec 02 '22 14:12

Andrew Brooke