String.Format Phone Numbers with Extension

10,634

Solution 1

I think you'll have to break your phoneDigits string into the first 10 digits and the remainder.

//[snip]
else if (phoneDigits.ToString().Length > 10)
        {
            return String.Format("{0:(###) ###-#### x}{1}", phoneDigits.Substring(0,10), phoneDigits.Substring(10) );
        }
//[snip]

Solution 2

Trying to squeeze it into 1 line, I came up with this.

var phoneNumber = "(999) 555-4455 ext123";
phoneNumber = Regex.Replace(phoneNumber, "(.*?)([+]\\d{1,3})?(.*?)(\\d{3})(.*?)(\\d{3})(.*?)(\\d{4})([ ]+)?(x|ext)?(.*?)(\\d{2,5})?(.*?)$", "$2 $4 $6 $8 $10$12").Trim().Replace("ext","x");

If it starts with +# it will leave that alone. It will then look for blocks of numbers. 3,3,4 then it looks for ext or x for extension and another 2-5 numbers. At that point you can format it anyway you like, I chose spaces.

1234567890 -> '123 456 7890'

(123)456.7890 -> '123 456 7890'

+1 (999)555-4455 ext123 -> '+1 999 555 4455 x123'

Solution 3

I'd suggest treating it as a string of digits, not a number. You would then use Substring explicitly to break out the parts.

Share:
10,634

Related videos on Youtube

ChiliYago
Author by

ChiliYago

Updated on June 04, 2022

Comments

  • ChiliYago
    ChiliYago almost 2 years

    I am trying to create a an function that formats US phone numbers -- hopefully without looping through each digit.

    When 10 digits are passed in all is fine. How ever when more than 10 digits are passed in I want the String.Format method to append the extension digits on the right. For example:

    When 14 digits passed in the result should be:(444)555-2222 x8888 When 12 digits passed in the result should be:(444)555-2222 x88 etc. However what I get with my current attempt is: Passing in 12 digits returns this string '() -949 x555444433'

    here is what I have so far.

    public static string _FormatPhone(object phonevalue)
    {
        Int64 phoneDigits;
    
        if (Int64.TryParse(phonevalue.ToString(), out phoneDigits))
        {
            string cleanPhoneDigits = phoneDigits.ToString();
            int digitCount = cleanPhoneDigits.Length;
    
            if (digitCount == 10)
                return String.Format("{0:(###) ###-####}", phoneDigits);
            else if (digitCount > 10)
                return String.Format("{0:(###) ###-#### x#########}", phoneDigits);
            else
                return cleanPhoneDigits;
        }
    
        return "Format Err#";
    }
    

    Thanks in advance.

  • Steven Sudit
    Steven Sudit about 14 years
    I would consider regexp to be complete overkill for this example.
  • Steven Sudit
    Steven Sudit about 14 years
    Ok, but if you're going to go down the Substring path, why stop there?
  • Ahmad Mageed
    Ahmad Mageed about 14 years
    This doesn't handle the case where 10 digits are used and $4 is empty, which returns the result with a trailing "x": 312-588-2300 x
  • BlackICE
    BlackICE about 14 years
    @Steven Why, a phone number is after all a regular expression?
  • Ray
    Ray about 14 years
    @Ahmad - good point - I am editing my response with another regex approach, using a MatchEvaluator just to see how it works.
  • ChiliYago
    ChiliYago about 14 years
    This seems straight forward enough. else if (phoneDigitString.Length > 10) { return String.Format( "({0}) {1}-{2} x{3}", phoneDigitString.Substring(0, 3), phoneDigitString.Substring(3, 3), phoneDigitString.Substring(3, 4), phoneDigitString.Substring(10)); }
  • Steven Sudit
    Steven Sudit about 14 years
    It's a safe bet that the Substring way would be faster.
  • Steven Sudit
    Steven Sudit about 14 years
    @David: Because we have all the digits just sitting there. Nothing to search, nothing to match. All we need to do is mix them in with some formatting, and that's easy enough with a StringBuffer.
  • kiev
    kiev almost 14 years
    This works Chili, but what if you have a number like 18005551212? Can a valid phone number start with 1 in the US or is it always used like 1 (800) 555-1212 ? or 1 (212) 515-1212 ?