格式化字符串/电话号码

时间:2011-05-26 17:40:21

标签: c# model-view-controller

我想使用C#格式化电话号码。格式为(XX)YYY-ZZZZ或(XXX)YYY-ZZZZ。所以,我需要 格式化正确的七位数字,然后保留两个或三个区域代码。

{编辑}

手机保存为长度为9或10的普通字符串/ VARChar

请建议解决方案。

感谢。

5 个答案:

答案 0 :(得分:2)

如果我理解正确,您需要一个接收int的函数,并返回一个string,其中包含该格式的fone编号。如果是这样,你可以做类似的事情:

public string GetPhoneNumber(int number) {
    string n = number.ToString();
    if (n.Length == 9)
        return "(" + n.Substring(0, 2) + ") " + n.Substring(2, 3) + "-" + n.Substring(5, 4);
    else
        return "(" + n.Substring(0, 3) + ") " + n.Substring(3, 3) + "-" + n.Substring(6, 4);
}

答案 1 :(得分:1)

假设电话号码存储为长号,并且是9位或10位数字,您可以这样做:

if (number > 999999999L)
{
     return String.Format("{0:(###) ###-####}", number);
}
else
{
     return String.Format("{0:(##) ###-####}", number);
}

当然,只有在数字长度为9或10位时才有效。

答案 2 :(得分:1)

这将采用任何字符串,从右侧放置到前10个数字

(?xxx)xxx-xxxx

格式化字符串。

    public string FormatPhone(string input)
    {
        List<char> chars = new List<char>();
        if (input.Length < 9) throw new ArgumentException("Not long enough!");
        for (int i = input.Length - 1; i >= 0; i--)
        {
            if (Char.IsNumber(input[i])) chars.Add(input[i]);
            switch (chars.Count)
            {
                case 4:
                    chars.Add('-');
                    break;
                case 8:
                    chars.Add(' ');
                    chars.Add(')');
                    break;
                case 13:
                    i = 0;
                    break;
            }
        }
        chars.Add('(');
        chars.Reverse();
        return new string(chars.ToArray());
    }

答案 3 :(得分:0)

电话号码是存储为字符串还是数值。如果它存储为整数值,则应该这样做:

string formattedPhone = rawNumber.ToString( "(#00) 000-0000" ) ;

如果它存储为字符串,你需要查看长度并开始砍掉它,因此:

static string FormatAsPhoneNumber( string s )
{
  if ( s == null ) throw new ArgumentNullException() ;
  if ( s.Length > 10 ) throw new ArgumentOutOfRangeException() ;

  StringBuilder sb = new StringBuilder() ;
  int           p  = 0 ;
  int           remaining = s.Length ;

  if ( remaining > 7 )
  {
    int areaCodeLength = remaining - 7 ;

    sb.Append("(").Append(s.Substring(p,areaCodeLength)).Append(") ") ;

    p         += areaCodeLength ;
    remaining -= areaCodeLength ;

  }
  if ( remaining > 4 )
  {
    int exchangeLength = remaining - 4 ;

    sb.Append(s.Substring(p,exchangeLength)).Append("-") ;

    p         += exchangeLength ;
    remaining -= exchangeLength ;

  }

  sb.Append(s.Substring(p) ) ;

  string formatted = sb.ToString() ;
  return formatted ;
}

结果:

Raw         Formatted
---------- --------------
1          1
12         12
123        123
1234       1234
12345      1-2345
123456     12-3456
1234567    123-4567
12345678   (1) 234-5678
123456789  (12) 345-6789
1234567890 (123) 456-7890

答案 4 :(得分:0)

static string FormatPhoneNumber( string phoneNumber ) {

   if ( String.IsNullOrEmpty(phoneNumber) )
      return phoneNumber;

   Regex phoneParser = null;
   string format     = "";

   switch( phoneNumber.Length ) {

      case 5 :
         phoneParser = new Regex(@"(\d{3})(\d{2})");
         format      = "$1 $2";
       break;

      case 6 :
         phoneParser = new Regex(@"(\d{2})(\d{2})(\d{2})");
         format      = "$1 $2 $3";
       break;

      case 7 :
         phoneParser = new Regex(@"(\d{3})(\d{2})(\d{2})");
         format      = "$1 $2 $3";
       break;

      case 8 :
         phoneParser = new Regex(@"(\d{4})(\d{2})(\d{2})");
         format      = "$1 $2 $3";
       break;

      case 9 :
         phoneParser = new Regex(@"(\d{4})(\d{3})(\d{2})(\d{2})");
         format      = "$1 $2 $3 $4";
       break;

      case 10 :
         phoneParser = new Regex(@"(\d{3})(\d{3})(\d{2})(\d{2})");
         format      = "($1) $2 $3 $4";
       break;

      case 11 :
         phoneParser = new Regex(@"(\d{4})(\d{3})(\d{2})(\d{2})");
         format      = "($1) $2 $3 $4";
       break;

      default:
        return phoneNumber;

   }//switch

   return phoneParser.Replace( phoneNumber, format );

}//FormatPhoneNumber