How to format a string as a telephone number in C#

Please note, this answer works with numeric data types (int, long). If you are starting with a string, you’ll need to convert it to a number first. Also, please take into account that you’ll need to validate that the initial string is at least 10 characters in length.

From a good page full of examples:

String.Format("{0:(###) ###-####}", 8005551212);

    This will output "(800) 555-1212".

Although a regex may work even better, keep in mind the old programming quote:

Some people, when confronted with a
problem, think “I know, I’ll use
regular expressions.” Now they have
two problems.
–Jamie Zawinski, in comp.lang.emacs

Leave a Comment