title: How can I make phone numbers pretty/user friendly when their existing formats are inconsistent?
body: <p>I'm binding a textbox to a member of a class, and I need to tweak the appearance of the phone number so that it's easier to read (the user doesn't want to see values such as "1234567890" or "+01234567890"). So, I've got this code:</p>
  
  <pre><code>var bindingPhone = new Binding("Text", platypusInfo, "Phone1", true);
  bindingPhone.Format += phoneBinding_Format;
  textBoxPhoneNum1.DataBindings.Add(bindingPhone);
  
  ...
  
  private void phoneBinding_Format(object sender, ConvertEventArgs e) {
      e.Value = ??How can I deal with this??
  }
  </code></pre>
  
  <p>But the phone values, although <em>usually</em> either "NNNNNNNNNN" (such as "1234567890") or "+NNNNNNNNNNN" (such as "+01234567890") can also appear in a number of other permutations, such as:</p>
  
  <pre><code> (NN) NNNN NNNN
  ++NNNNNNNNNNNNN
  +NNNNNNNNNNNNN
  +NN NNNNNNNNNNN
  NNNNNNNNNNNN
  </code></pre>
  
  <p>Is there anything I can do in phoneBinding_Format() that will make these phone numbers easier to read without breaking them into nonsensical parts, such as "43-4859-4365" instead of "434-859-4365"?</p>
  
  <h2>UPDATE</h2>
  
  <p>Due to these factors:</p>
  
  <p>1) I'm working on several projects simultaneously and need to get back to another one
  2) Our two most common formats comprise the lion's share of our phone numbers
  3) This is just a "nice feature" not a "must-have" feature</p>
  
  <p>...I've settled on the following for now, based on a Jon Skeet answer:</p>
  
  <pre><code>private void phoneBinding_Format(object sender, ConvertEventArgs e)
  {
      const int UK_PHONE_LEN = 9;                     // +NNNNNNNN 
       const int US_PHONE_FORMAT_LEN = 10;             // NNNNNNNNNN
      const int COMMON_INTERNATIONAL_FORMAT_LEN = 12; //+NNNNNNNNNNN 
  
      string phone;
      string area;
      string major;
      string minor;
      string intl_firstsegment;
      string intl_secondsegment;
      string intl_thirdsegment;
      string intl_fourthsegment;
      string intl_fifthsegment;
  
      if (e.Value.ToString().Length == US_PHONE_FORMAT_LEN)
      {
          phone = e.Value.ToString();
          area = phone.Substring(0, 3);
          major = phone.Substring(3, 3);
          minor = phone.Substring(6);
          e.Value = string.Format("{0}-{1}-{2}", area, major, minor); 
      } 
  else if ((e.Value.ToString().Length == UK_PHONE_LEN) &amp;&amp; (e.Value.ToString()[0] == '+')) {
      phone = e.Value.ToString();
      intl_firstsegment = phone.Substring(0, 2);
      intl_secondsegment = phone.Substring(2, 3);
      intl_thirdsegment = phone.Substring(5);
      e.Value = string.Format("+{0}-{1}-{2}", intl_firstsegment, intl_secondsegment, intl_thirdsegment);
  } 
  else if ((e.Value.ToString().Length == COMMON_INTERNATIONAL_PHONE_LEN) &amp;&amp; (e.Value.ToString()[0] == '+')) 
      {
          phone = e.Value.ToString();
          intl_firstsegment = phone.Substring(0, 2);
          intl_secondsegment = phone.Substring(2, 2);
          intl_thirdsegment = phone.Substring(4, 3);
          intl_fourthsegment = phone.Substring(7, 2);
          intl_fifthsegment = phone.Substring(9);
          e.Value = string.Format("+{0}-{1}-{2}-{3}-{4}", intl_firstsegment, intl_secondsegment, intl_thirdsegment, intl_fourthsegment, intl_fifthsegment);
      }
  }
  </code></pre>
  
  <p>BTW, an interesting thing happened on the way to breakpoint nirvana: I originally had these tests (1st character is a plus sign and length is the expected) reversed, and got: <em>System.IndexOutOfRangeException was unhandled by user code
    Message=Index was outside the bounds of the array.</em></p>
  
  <p>Reversing the condition so that length was checked first (which naturally doesn't fail when length is 0/string is empty) fixed it (since then no attempt is made to access char 0).</p>