C# 國際手機號類別封裝 - 使用nuget Global Phone 解析國際手機號
c#
1. package-install GlobalPhone
2. Phone.cs聽
聽 聽
public class Phone { public string CountryCode { get; set; } public string InternationalNumber { get; set; } public string NationalNumber { get; set; } public string Territory { get; set; } public bool IsValid { get; set; } public Phone() { } private Phone(global::GlobalPhone.Number number) : this() { CountryCode = number.CountryCode; InternationalNumber = number.InternationalString; NationalNumber = number.NationalFormat; Territory = number.Territory.Name; IsValid = number.IsValid; } private static bool __libraryLoaded; private static global::GlobalPhone.Database __databse; private static void LoadStaticContent() { if (!__libraryLoaded) { try { var assembly = Assembly.GetExecutingAssembly(); var dataStream = assembly.GetManifestResourceStream("Common.GlobalPhone.data.json"); if (dataStream != null) { var reader = new StreamReader(dataStream); var text = reader.ReadToEnd(); global::GlobalPhone.GlobalPhone.DbText = text; __databse = global::GlobalPhone.Database.Load(text); } } catch (Exception ex) { throw; } __libraryLoaded = true; } } private static readonly object __globalPhoneLock = new object(); private static global::GlobalPhone.Region TrnaslateRegion(String countryCode) { if (string.IsNullOrEmpty(countryCode)) { return null; } lock (__globalPhoneLock) { return __databse.TryGetRegion(countryCode); } } private static global::GlobalPhone.Territory TranslateTerritory(String territoryName) { if (string.IsNullOrEmpty(territoryName)) { return null; } lock (__globalPhoneLock) { return __databse.TryGetTerritory(territoryName); } } public static Phone TryParseWithCountryCode(string number, string countryCode) { LoadStaticContent(); var region = TrnaslateRegion(countryCode); if (region != null) { var territory = region.Territories.FirstOrDefault(); if (territory != null) { return TryParse(number, territory.Name); } } return null; } public static Phone TryParse(string number, string territoryName = null) { LoadStaticContent(); if (string.IsNullOrEmpty(number)) { return null; } var globalNumber = global::GlobalPhone.GlobalPhone.TryParse(number, territoryName); if (globalNumber != null && globalNumber.IsValid) { return new Phone(globalNumber); } //In scenario such as 6597531150 we need to help the library to understand that there is already the country code as a prefix var territory = TranslateTerritory(territoryName); if (territory != null) { //If the number start with same country code, we will try to add the plus and parse it again if (number.StartsWith(territory.CountryCode)) { globalNumber = global::GlobalPhone.GlobalPhone.TryParse(string.Format("+{0}", number), territoryName); if (globalNumber != null && globalNumber.IsValid) { return new Phone(globalNumber); } } } return null; } }
登入後複製
以上就是C# 国际手机号类封装 - 使用nuget Global Phone 解析国际手机号的内容,更多相关内容请关注PHP中文网(www.php.cn)!
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前
By 尊渡假赌尊渡假赌尊渡假赌
刺客信條陰影:貝殼謎語解決方案
2 週前
By DDD
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前
By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

使用 C# 的 Active Directory 指南。在這裡,我們討論 Active Directory 在 C# 中的介紹和工作原理以及語法和範例。

多線程和異步的區別在於,多線程同時執行多個線程,而異步在不阻塞當前線程的情況下執行操作。多線程用於計算密集型任務,而異步用於用戶交互操作。多線程的優勢是提高計算性能,異步的優勢是不阻塞 UI 線程。選擇多線程還是異步取決於任務性質:計算密集型任務使用多線程,與外部資源交互且需要保持 UI 響應的任務使用異步。
