Can anybody help me to solve this problem ?
I want "-"
between phone numbers. I tried . It shows XXX-XXX-XXX-X
Formats only . But i want XXX-XXX-XXXX
Format .
Here Below I have mentioned my code.
Declare This under Oncreate
TextView TxtDocPhone=(TextView)findViewById(R.id.TxtDocPhone);
String numbersOnly = keepNumbersOnly(Phone);
String ProviderPhone = formatNumbersAsCode(numbersOnly);
Then call it
private String keepNumbersOnly(CharSequence s) {
return s.toString().replaceAll("[^0-9]", "");
}
private String formatNumbersAsCode(CharSequence s) {
int groupDigits = 0;
String tmp = "";
for (int i = 0; i < s.length(); ++i) {
tmp += s.charAt(i);
++groupDigits;
if (groupDigits == 3) {
tmp += "-";
// tmp += "";
groupDigits = 0;
}
}
return tmp;
}
Please guide me the correct way to achieve my objective.
Try this way,hope this will help you to solve your problem.
String formattedNumber = PhoneNumberUtils.formatNumber(unformattedNumber);
Take a look at PhoneNumberUtils for more options.
Finally i solve this issue . Just add below this in formatNumbersAsCode
function .
return String.format("%s-%s-%s",s.subSequence(0,3),s.subSequence(3,6),s.subSequence(6,10));
You can try:
private String formatNumbersAsCode(CharSequence s) {
return String.format("%s-%s-%s",s.substring(0,3),s.substring(3,6),s.substring(6));
}
You best way to go is to use Regular Expressions .
You know your phone number should have the format XXX-XXX-XXXX
, so you just write an appropriate RegEx for this, and you are fine!
用space (" ")
格式化,然后替换所有的"-"
字符。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.