Skip to main content
 首页 » 编程设计

c#手机格式扩展方法

2024年02月23日14cloudgamer

我正在编写一个扩展方法,它接受一串数字(即 4083239382)并根据国家代码将其转换为电话号码:

public static string ToPhoneFormat(this string ThePhone, int CountryCode) 
{ 
 string ThisFormat = null; 
 if (CountryCode == 1) { ThisFormat = string.Format("{0:###-###-####}",ThePhone); } 
 return ThisFormat; 
} 

出于某种原因,我没有得到想要的结果。有什么建议吗?

谢谢。

请您参考如下方法:

这可能与您将电话号码作为字符串传递有关。首先将其解析为 long。

string ThisFormat = null; 
if (CountryCode == 1) { ThisFormat = string.Format("{0:###-###-####}", long.Parse(ThePhone)); } 
return ThisFormat;