Formatting string to phone number...

I simple way to format a string to a phone number is by doing to following:

double result = default(double);
if (double.TryParse(from.PhoneNumber, out result))
{
    to.PhoneNumber = string.Format("{0: (###) ###-####}", result);
}

This format will "9908675309" to "(990) 867-5309". In order for the string formatting to work the string to be formatted must be converted to a decimal data type.

Now, I'll admit, there are many ways to skin the proverbial cat (like using a regular expression)...but this is just another way of doing this...

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.