Java电话号码格式API [英] Java phone number format API

查看:153
本文介绍了Java电话号码格式API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含数百万个电话号码的数据库,可以进行免费格式化。即,UI不强制执行任何约束,并且用户正在输入他们想要的任何内容。

I have a database with millions of phone numbers with free-for-all formatting. Ie, the UI does not enforce any constraints and the users are typing in whatever they want.

我正在寻找的是一个可以做到最好的Java API努力将这些转换为一致的格式。理想情况下,API将采用自由文本值和国家/地区代码并生成有效的国际电话号码或抛出异常。

What I'm looking for is a Java API that can make a best-effort to convert these into a consistent format. Ideally, the API would take the free text value and a country code and produce a valid international phone number or throw an exception.

例如,系统中的电话号码可能看起来像以下任何一种:

For example, a phone number in the system might look like any of the following:

(555) 478-1123
555-478-1123    
555.478.1123
5554781123

鉴于美国的国家/地区,API会产生价值所有这些都是+1(555)478-1123。确切的格式无关紧要,只要它是一致的。

Given the country of US, the API would produce the value "+1 (555) 478-1123" for all these. The exact format does not matter, as long as it's consistent.

系统中也有数字没有区号,例如478-1123。在这种情况下,我会期望NoAreaCodeException或类似的东西。

There are also numbers in the system without area codes, such as "478-1123". In that case, I would expect a NoAreaCodeException, or something similar.

还可能有abc这样的数据,这也应该抛出异常。

There could also be data such as "abc", which should also throw exceptions.

当然,我发布的例子有无数种变化,以及国际电话号码的巨大复杂性,它们具有相当复杂的验证规则。这就是为什么我不考虑自己动手。

Of course, there are countless variations of the examples I have posted, as well as the enormous complication of international phone numbers, which have quite complicated validation rules. This is why I would not consider rolling my own.

有没有人见过这样的API?

Has anyone seen such an API?

推荐答案

您可以自己编写(美国电话#格式):

You could write your own (for US phone # format):


  • 从中删除任何非数字字符字符串

  • 检查剩余的字符串是否为十个字符

  • 将括号括在前三个字符周围,在第六个和第七个字符之间加一个短划线。

  • 在字符串前加上+1

  • Strip any non-numeric characters from the string
  • Check that the remaining string is ten characters long
  • Put parentheses around the first three characters and a dash between the sixth and seventh character.
  • Prepend "+1 " to the string


Google最近发布 libphonenumber 用于解析,格式化,存储和验证国际电话号码。

Google recently released libphonenumber for parsing, formatting, storing and validating international phone numbers.

这篇关于Java电话号码格式API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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