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.
精彩评论