Argentini.Halide.H3Text.FormatTelephoneNumber C# (CSharp) Method

FormatTelephoneNumber() public static method

Formats a telephone number. For U.S. 10-digit numbers, if a leading "1" is included, it is stripped prior to formatting.
public static FormatTelephoneNumber ( string phoneNumber, TelephoneFormat telephoneFormat ) : String
phoneNumber string The phone number.
telephoneFormat TelephoneFormat TelephoneFormat constant which defines how the telephone number should be formatted.
return String
        public static String FormatTelephoneNumber(string phoneNumber, TelephoneFormat telephoneFormat)
        {
            string returnVal = String.Empty;

            if (phoneNumber.Length > 0)
            {
                for (int x = 0; x < phoneNumber.Length; x++)
                {
                    char strChar = Convert.ToChar(Mid(phoneNumber, x, 1));

                    if ((int) strChar > 47 && (int) strChar < 58) returnVal += Mid(phoneNumber, x, 1);
                }
            }

            if (returnVal.Length == 11 && returnVal.StartsWith("1"))
            {
                returnVal = returnVal.TrimStart('1');
            }

            if (returnVal.Length == 10)
            {
                if (telephoneFormat == TelephoneFormat.Full)
                {
                    returnVal = string.Format("({0}) {1}-{2}", returnVal.Substring(0, 3), returnVal.Substring(3, 3), returnVal.Substring(6));
                }

                if (telephoneFormat == TelephoneFormat.Dots)
                {
                    returnVal = string.Format("{0}.{1}.{2}", returnVal.Substring(0, 3), returnVal.Substring(3, 3), returnVal.Substring(6));
                }

                if (telephoneFormat == TelephoneFormat.Hyphens)
                {
                    returnVal = string.Format("{0}-{1}-{2}", returnVal.Substring(0, 3), returnVal.Substring(3, 3), returnVal.Substring(6));
                }
            }

            return returnVal;
        }