【问题标题】:How to format phone number to national format without national prefix using PhoneNumberUtils library?如何使用 PhoneNumberUtils 库将电话号码格式化为没有国家前缀的国家格式?
【发布时间】:2017-01-22 22:57:00
【问题描述】:

我正在尝试将以前 E164 格式的电话号码格式化为没有国家前缀号码的国家格式,我正在使用 PhoneNumberUtils 来做到这一点:

public static String getNationalPhone(String phoneE164Formatted) {
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    String phoneNumber = phoneE164Formatted;
    try {
        Phonenumber.PhoneNumber numberProto = phoneUtil.parseAndKeepRawInput(phoneE164Formatted, "");
        phoneNumber = phoneUtil.format(numberProto, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
    } catch (NumberParseException e) {
        e.printStackTrace();
    }

    return phoneNumber;
}

使用该代码,我得到了我不想要的国家前缀。例如,将这个数字“+526143342293”格式化为国家格式给了我这个结果:“01 614 334 22 93”,其中“01”是我想要抑制的。

【问题讨论】:

    标签: android libphonenumber phonenumberutils


    【解决方案1】:

    https://github.com/brick/phonenumber/blob/master/src/PhoneNumber.php

    这些答案在php中,你可以转换成你自己的。

    public function getCountryCode()
    {
        return (string) $this->phoneNumber->getCountryCode();
    }
    

    转换上述代码将得到所需格式的国家号码。

    【讨论】:

      猜你喜欢
      • 2016-07-27
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多