我可以通过手机获取联系电话。我想知道如何从中删除国家代码。也就是说,如果用户的电话号码带有国家代码 (+91-9876543210),我需要没有前缀国家代码的电话号码 (9876543210)。
提前致谢。
最佳答案
我建议使用 libphonenumber轻松解析电话号码。您可以通过这种方式拆分国家代码和电话号码。
try {
// phone must begin with '+'
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber numberProto = phoneUtil.parse("+91-9876543210", "");
int countryCode = numberProto.getCountryCode();
long nationalNumber = numberProto.getNationalNumber();
Log.i("code", "code " + countryCode);
Log.i("code", "national number " + nationalNumber);
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
关于java - 如何在没有国家代码的情况下获得电话联系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31175672/