问题描述
我有一个正在使用的电话号码作为唯一标识系统。出于这个原因,我要为他们在用一种标准化的格式来格式化所有的电话号码。因为我已经在我的源数据没有控制,我需要分析出这些数字我和它们添加到我的数据库之前对其进行格式化。
I have a system which is using phone numbers as unique identifiers. For this reason, I want to format all phone numbers as they come in using a normalized format. Because I have no control over my source data, I need to parse out these numbers myself and format them before adding them to my DB.
我要写一个分析器,可以读取电话号码,并输出标准化的电话格式,但在此之前我做的,我想知道是否有人知道任何$ P $的对现有的库,我可以用它来格式化电话号码
I'm about to write a parser that can read phone numbers in and output a normalized phone format, but before I do I was wondering if anyone knew of any pre-existing libraries I could use to format phone numbers.
如果没有pre-现有的库在那里,我应该牢记哪些东西创建此功能时,可能不是很明显?
If there are no pre-existing libraries out there, what things should I be keeping in mind when creating this feature that may not be obvious?
虽然我的系统只与美国的数字打交道,现在,我打算尝试以包括国际号码的支持,以防万一,因为有它,将需要一个机会。
Although my system is only dealing with US numbers right now, I plan to try to include support for international numbers just in case since there is a chance it will be needed.
修改我忘了提我使用C#.NET 2.0。
Edit I forgot to mention I'm using C#.NET 2.0.
推荐答案
您可以使用 libphonenumber
来自谷歌。这里有一个博客帖子:
You could use libphonenumber
from Google. Here's a blog post:
的
解析数是因为安装的NuGet包,然后做这个一样简单:
Parsing numbers is as easy as installing the NuGet package and then doing this:
var util = PhoneNumberUtil.GetInstance();
var number = util.Parse("555-555-5555", "US");
然后,您可以格式化数字是这样的:
You can then format the number like this:
util.Format(number, PhoneNumberFormat.E164);
libphonenumber
支持多种格式不是E.164等。
libphonenumber
supports several formats other than E.164.
这篇关于电话号码规范化:任何pre-现有的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!