从电话中提取code国[libphonenumber] [英] Extract code country from phone number [libphonenumber]

查看:572
本文介绍了从电话中提取code国[libphonenumber]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串像这样: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的(<一href="https://groups.google.com/forum/?hl=en&fromgroups#!forum/libphonenumber-discuss">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());
}

这篇关于从电话中提取code国[libphonenumber]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆