从电话中提取code国[libphonenumber]电话、code、libphonenumber

2023-09-04 23:16:35 作者:太可爱会被拐了

我有一个字符串像这样:33123456789(法语电话号码)。我想提取全国code(+33)不知道这个国家。例如,它应该工作,如果我有另一个电话来自另一个国家。我用的是谷歌图书馆的https://$c$c.google.com/p/libphonenumber/

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 another phone from another country. I use the google library https://code.google.com/p/libphonenumber/.

如果我知道这个国家,这是很酷,我可以找国code:

If I know the country, it is cool I can find the country code :

PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
int countryCode = phoneUtil.getCountryCodeForRegion(locale.getCountry());

但我没有找到一个方法来解析字符串,而不知道国家。

but I don't find a way to parse a string without to know the country.

推荐答案

好了,所以我加入了谷歌组libphonenumber的(https://groups.google.com/forum/?hl=en&fromgroups#!forum/libphonenumber-discuss ),我问过一个问题。

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());
}