Extract code country from phone number [libphonenumber]

前端 未结 7 1805
情深已故
情深已故 2020-11-27 13:21

I have a string like this : +33123456789 (french phone number). I want to extract the country code (+33) without knowing the country. For example, it should work if i have a

7条回答
  •  忘掉有多难
    2020-11-27 13:54

    Okay, so I've joined the google group of libphonenumber ( https://groups.google.com/forum/?hl=en&fromgroups#!forum/libphonenumber-discuss ) and I've asked a question.

    I don't need to set the country in parameter if my phone number begins with "+". Here is an example :

    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    try {
        // phone must begin with '+'
        PhoneNumber numberProto = phoneUtil.parse(phone, "");
        int countryCode = numberProto.getCountryCode();
    } catch (NumberParseException e) {
        System.err.println("NumberParseException was thrown: " + e.toString());
    }
    

提交回复
热议问题