java - Replace english digit with persian digit in String except URL's -


i wrote custom textview persian string, textview should replace english digit's persian digit's

public class persiantextview extends textview {      public persiantextview(context context) {         super(context);     }      public persiantextview(context context, attributeset attrs) {         super(context, attrs);     }      public persiantextview(context context, attributeset attrs, int defstyleattr) {         super(context, attrs, defstyleattr);     }      @override     public void settext(charsequence text, buffertype type) {         string t = text.tostring();         t = t.replaceall("0", "۰");         t = t.replaceall("1", "۱");         t = t.replaceall("2", "۲");         t = t.replaceall("3", "۳");         t = t.replaceall("4", "۴");         t = t.replaceall("5", "۵");         t = t.replaceall("6", "۶");         t = t.replaceall("7", "۷");         t = t.replaceall("8", "۸");         t = t.replaceall("9", "۹");         super.settext((charsequence)t, type);     } } 

this view work correctly, if set text html, links contain english digits not show link! easiest way solve problem?

one needs in form pattern matching on links.

private static final pattern digit_or_link_pattern =     pattern.compile("(\\d|https?:[\\w_/+%?=&.]+)"); // pattern:          (dig|link                 )  private static final map<string, string> persian_digits = new hashmap<>(); static {    persian_digits.put("0", "۰");    persian_digits.put("1", "۱");    persian_digits.put("2", "۲");    persian_digits.put("3", "۳");    persian_digits.put("4", "۴");    persian_digits.put("5", "۵");    persian_digits.put("6", "۶");    persian_digits.put("7", "۷");    persian_digits.put("8", "۸");    persian_digits.put("9", "۹"); }  public static string persiandigits(string s) {     stringbuffer sb = new stringbuffer();     matcher m = digit_or_link_pattern.matcher(s);     while (m.find()) {         string t = m.group(1);         if (t.length() == 1) {             // digit.             t = persian_digits.get(t);         }         m.appendreplacement(sb, t);     }     m.appendtail(sb);     return sb.tostring(); } 

p.s.

depending on text, might better replace digits outside html tags, between > , <.

private static final pattern digit_or_link_pattern =     pattern.compile("(\\d|<[^>]*>)",         pattern.dotall|pattern.multiline); 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -