2012-09-22 79 views
6

我知道的谷歌libphonenumber解析庫的C#端口: http://code.google.com/p/libphonenumber/解析電話號碼給他們的部分

我需要的是採取一個手機串號,並把它分解成相應的碎片,國家代碼,區號,前綴,數字和擴展名。

這個庫可以用來做到這一點嗎?如果是這樣,有人可以在C#中發佈一個簡單的測試來做到這一點?我不知道如何在文檔中做到這一點。

順便說一句,它們可以是國內的或國際的。

回答

6

libphonenumber庫將解析一個數字並驗證它是否匹配國內和國際編號的已知模式。它會告訴你國家代碼和國內或國際上任何給定號碼的正確撥號模式。

除此之外,它不會將其解析爲組成部分。沒有區號,前綴,號碼,擴展分析。

它是開源的,所以如果你需要這樣做,它可能是一個很好的起點,但我相信這將是一個巨大的任務。

+0

然後,你知道一個圖書館會這樣做嗎? – MB34

+0

@ MB34,對不起,我沒有。 –

+0

您是否看過2008年CodeProject上的庫?我不確定它是否也這樣做。 http://www.codeproject.com/KB/cs/PhoneNumberStruct.aspx?display=Print – MB34

3

帕特里克Mezard慷慨移植庫,以C#:

https://bitbucket.org/pmezard/libphonenumber-csharp/wiki/Home

有關使用方法,你可以看看官方網站:

http://code.google.com/p/libphonenumber/

Java代碼可以直接翻譯成C#。例如:

的Java

String swissNumberStr = "044 668 18 00" 
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance(); 
try { 
    PhoneNumber swissNumberProto = phoneUtil.parse(swissNumberStr, "CH"); 
} catch (NumberParseException e) { 
    System.err.println("NumberParseException was thrown: " + e.toString()); 
} 

C#

String swissNumberStr = "044 668 18 00"; 
PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance(); 
try 
{ 
    PhoneNumber swissNumberProto = phoneUtil.Parse(swissNumberStr, "CH"); 
    Console.WriteLine(swissNumberProto.CountryCode); 
} 
catch (NumberParseException e) 
{ 
    Console.WriteLine("NumberParseException was thrown: " + e.ToString()); 
} 

好運。

更新:

更多的例子:http://code.google.com/p/libphonenumber/source/browse/#svn/trunk/java/libphonenumber/test/com/google/i18n/phonenumbers

如果你沒有看到你所需要的,那麼我想你可以實現它自己。

+0

我沒有看到解析出部件。如果我通過國家代碼「CH」,爲什麼我需要使用'swissNumberProto.CountryCode?' – MB34