开发者

String.Format is not formatting phone number

String.Format("{0:###-###-#开发者_如何学运维###}", customer.ContactHome); //NOT working (9891205789)

BUT

String.Format("{0:###-###-####}", Convert.ToInt64(customer.ContactHome)); //Works fine (989-120-5789)

but I don't want to Cast phone no due to some reason. How can I format phone no without casting?


If customer.ContactHome is string, you can do:

Regex.Replace(customer.ContactHome, "(\d\d\d)(\d\d\d)(\d\d\d\d)", "$1-$2-$3");

or

customer.ContactHome.Substring(0,3) + "-" +
customer.ContactHome.Substring(3,3) + "-" + 
customer.ContactHome.Substring(6,4);


You are using numeric formating ("{0:###-###-####}") on a string customer.ContactHome that's why it's not working.


If customer.ContactHome is a string, it will not get formatted using a numeric format, as it is already a string and the format string expect a number.


If you dont want to cast your value and also to use regex, you can always use String methods like Substring to get a formatted string value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜