android - Format a french phone number -
i have format "0612345678" "06 12 34 56 78".
i'm using :
private string formattel(string number) { try { string formattednumber = ""; int i; (i = 0; < 10; += 2) { formattednumber += number.substring(i, + 2) + " "; } return formattednumber.substring(0, formattednumber.length() - 1); } catch (indexoutofboundsexception e) { return number; } }
it works on emulator running api 16 (jellybean 4.1.1) : on it, result looks want ("06 12 34 56 78") on own phone, (api 22, lollipop 5.1.1), doesn't work : result "0 61 23 45 67" whereas method same !
i did research , i've found string.format method. it's here need help, it's hard me... !
thank in advance :d
edit :
working method, using libphonenumber :
private string formattel(string number) { string formattednumber = number; try { phonenumberutil phonenumberutil = phonenumberutil.getinstance(); phonenumber.phonenumber numberproto; numberproto = phonenumberutil.parse(number, "fr"); formattednumber = phonenumberutil.format(numberproto, phonenumberutil.phonenumberformat.national); } catch (numberparseexception e) { e.printstacktrace(); } return formattednumber; }
thank ye lin aung !
i've tried code on lollipop phone , works expect.
alternatively, i'd suggest use libphonenumber parse phone numbers whenever comes phone number. here's how done.
try { phonenumberutil phoneutil = phonenumberutil.getinstance(); phonenumber.phonenumber numberproto; numberproto = phoneutil.parse("+330612345678", ""); log.i("code", "code " + phoneutil.format(numberproto, phonenumberutil.phonenumberformat.national)); } catch (numberparseexception e) { e.printstacktrace(); }
which result
i/code﹕ code 06 12 34 56 78
Comments
Post a Comment