Java 如何将手机号码拆分为国家码、区号和本地号码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2543938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:06:12  来源:igfitidea点击:

How to split mobile number into country code, area code and local number?

javalocalizationformatting

提问by Vivart

How to split mobile number into country code, area code and local number? e.g +919567123456 after split

如何将手机号码拆分为国家码、区号和本地号码?例如 +919567123456 拆分后

country code = 91

国家代码 = 91

area code = 9567

区号 = 9567

local number = 123456

本地号码 = 123456

采纳答案by Dean Harding

It's not possible to parse phone numbers with a simple algorithm, you need to use data tables populated with each country's rules - because each country delimits their phone numbers differently.

不可能用简单的算法解析电话号码,您需要使用填充了每个国家/地区规则的数据表 - 因为每个国家/地区对电话号码的分隔方式不同。

The country code is fairly easy, just use the data from from the Country calling codesarticle in wikipedia and build a table of all the unique country codes. Each country has a unique prefix, so that's easy.

国家/地区代码相当简单,只需使用来自维基百科国家/地区呼叫代码文章的数据并构建所有唯一国家/地区代码的表格即可。每个国家/地区都有唯一的前缀,所以这很容易。

But then you need to look up the rules for every country you want to support and extract the area code(s) using the rules for each country.

但是,您需要查找要支持的每个国家/地区的规则,并使用每个国家/地区的规则提取区号。

回答by chriszero

I think you will need something like a dictonary of country and area codes. because booth of them can have a different lenght. USA +1, Germany +49, even +6723. Same with the Areacodes..

我认为您将需要诸如国家和区号字典之类的东西。因为他们的展位可以有不同的长度。美国 +1,德国 +49,甚至 +6723。与区号相同..

回答by Piskvor left the building

The answer very much depends on the country. There is no universalrule saying "this is country code, this is area code, this is local number". The only information that can be gained universally is the country number (and even that can be 1-4 digits long); then you need to consult the specific country's ruleset.

答案在很大程度上取决于国家。没有通用规则说“这是国家代码,这是区号,这是本地号码”。唯一可以普遍获得的信息是国家号码(甚至可以是 1-4 位数字);那么您需要查阅特定国家/地区的规则集。

For examples(like, "there are many different phone numbers in the given countries, but they all follow the same format"):

对于实例(如,“有在给定的国家的许多不同的电话号码,但它们都遵循相同的格式”):

  • +420123456789 is a (bogus) number in Czech Republic (country code +420 ), and the rest IS the local number (some countries use an undivided addressing space, although you could infer a few bits of data from the first 1-4 digits of the local number (e.g. "+420800 are toll-free numbers")). So, the only useful way to parse this number is into two parts, +420 123456789.
  • +18005551234 is a (probably also bogus) number in the US; according to the North American numbering plan, +1 is country code, 800 is area code (toll-free numbers), 555 is exchange code and 1234 is local number. You can then parse the number into four parts, +1 800 555 1234.
  • +420123456789 是捷克共和国的(伪造)号码(国家代码 +420 ),其余的是本地号码(某些国家/地区使用未分割的寻址空间,尽管您可以从前 1-4 位数字推断出几位数据)本地号码(例如“+420800 是免费电话号码”))。因此,解析此数字的唯一有用方法是将其分成两部分,+420 123456789
  • +18005551234 在美国是一个(可能也是假的)号码;根据北美编号方案,+1 是国家代码,800 是区号(免费电话号码),555 是交换代码,1234 是本地号码。然后,您可以将数字解析为四部分,+1 800 555 1234

回答by Steve Weet

As mentioned by various people you can not do this with simple string matching. The lengths of neither country nor area codes are fixed.

正如许多人所提到的,你不能用简单的字符串匹配来做到这一点。国家和地区代码的长度都不是固定的。

Having done this in the past we maintained a table similar in structure to the following :-

过去这样做后,我们维护了一个结构类似于以下的表:-

+------------+---------+-------+--------------+
|country_code|area_code|country|area          |
+------------+---------+-------+--------------+
|44          |1634     |UK     |Medway        |
|44          |20       |UK     |London        |
|964         |23       |Iraq   |Wasit (Al Kut)|
|964         |2412     |Iraq   |Unreal        |
+------------+---------+-------+--------------+

We then calculated the maximum length of area_code and country_code and checked the string by sub-stringing starting at the maximum length and working our way down until we found a match.

然后,我们计算了 area_code 和 country_code 的最大长度,并通过从最大长度开始进行子串并向下工作直到找到匹配项来检查字符串。

So given the number 441634666788

所以给出数字 441634666788

We would have started at the substring[1,7] (7 being the length of the longest country/area code combination), not found a match, then moved on to [1,6] and found the match for UK/Medway.

我们会从子字符串 [1,7](7 是最长国家/地区代码组合的长度)开始,没有找到匹配项,然后转到 [1,6] 并找到 UK/Medway 的匹配项。

Not very efficient but it worked.

效率不高,但有效。

EDIT

编辑

You could also try something like this but you would need to test it with a full data set or maybe even break it down into separate country and area code selects as it may not be very performant with your chosen DB.

您也可以尝试类似的方法,但您需要使用完整的数据集对其进行测试,或者甚至可能将其分解为单独的国家和地区代码选择,因为它在您选择的数据库中的性能可能不是很好。

 DECLARE @area_codes TABLE
(
    country_code VARCHAR(10),
    area_code VARCHAR(10),
    country VARCHAR(20),
    area VARCHAR(20),
    match_string VARCHAR(MAX),
    match_length INTEGER
)

INSERT INTO @area_codes VALUES ('44','1382','UK','Dundee', '441382%', 6)
INSERT INTO @area_codes VALUES ('44','1386','UK','Evesham', '441386%', 6)
INSERT INTO @area_codes VALUES ('44', '1', 'UK', 'Geographic numbers', '441%', 3)

DECLARE @number VARCHAR(MAX)
SET @number = '441386111111'

SELECT TOP 1 * 
FROM @area_codes 
WHERE @number LIKE match_string
ORDER BY match_length DESC

You would maintain the match_string and match_length fields through a trigger, taking care to cope with null area codes and index the table on the match_string column.

您将通过触发器维护 match_string 和 match_length 字段,注意处理空区码并在 match_string 列上索引表。

回答by Arne Burmeister

A very complex problem. First you need to determine the country code. Depending on the country code, the rest has to be splitted into area code and local number. But none of the three parts has a fixed length, not the hole number nor the area code and local part combination!

一个非常复杂的问题。首先,您需要确定国家/地区代码。根据国家代码,其余部分必须拆分为区号和本地号码。但是这三个部分都没有固定的长度,没有孔号,也没有区号和本地部分组合!

Example: 4930123456789

示例:4930123456789

  • 49 is the country code of Germany
  • 30 is the area code of Berlin
  • 123456789 is a local number in Berlin (no real one)
  • 49是德国的国家代码
  • 30是柏林的区号
  • 123456789 是柏林本地号码(不是真实号码)

Example: 493328123456

示例:493328123456

  • 49 is the country code of Germany
  • 3328 is the area code of Teltow
  • 123456 is a local number in Teltow (no real one)
  • 49是德国的国家代码
  • 3328是泰尔托的区号
  • 123456 是 Teltow 的本地号码(不是真实号码)

Example: 34971123456

示例:34971123456

  • 34 is the country code of Spain
  • 971 is the area code of Mallorca
  • 123456 is a local number on Mallorca (no real one)
  • 34是西班牙的国家代码
  • 971是马略卡的区号
  • 123456 是马略卡的本地号码(不是真实号码)

回答by Kevin Bourrillion

Don't maintain your own table of all this data! Use the "Java International Phone Number Utilities library v3.0", https://github.com/googlei18n/libphonenumber. This is what Google uses, and Google maintains it for you!

不要维护自己的所有这些数据表!使用“Java 国际电话号码实用程序库 v3.0”,https://github.com/googlei18n/libphonenumber。这是谷歌使用的,谷歌为您维护!

回答by g1smd

If you strive accurateUK data see also http://code.google.com/p/ofcom-csverter/for a complete list of UK area codes with corrections.

如果您追求准确的英国数据,另请参阅http://code.google.com/p/ofcom-csverter/以获取完整的英国区号列表和更正。