Formatting a telephone number

How do I format Employment.WorkPhoneNumber so that it’s in the format 123-456-7890?

1 Like

You have to use a Substring expression to break the telephone number into pieces and then concatenate the pieces together:

Employment.WorkPhoneNumber.Substring(0,3) + '-' + Employment.WorkPhoneNumber.Substring(3,3) + '-' + Employment.WorkPhoneNumber.Substring(6,4)

This will result in 123-456-7890. Note that if the telephone number does not contain at least 7 characters, this will result in a blank value.

Examples:

1234567890 = 123-456-7890
12345 = <blank value because there aren’t 7 characters>
123 456 7890 = 123- 45-6 78

You can use a regular expression to get the desired result. It handles spaces in the telephone number correctly.

Regex.Replace(Employment.WorkPhoneNumber, @"(\d{3})(\d{3})(\d{4})", "$1-$2-$3")

2 Likes