在我的数据库中,我存储的电话号码是“ 7279884545”。一个实心字符串,没有空格。

在存储电话号码信息的课程中,我有一个可以添加正确标点符号的函数。

public static String beautifyPhoneNumber(String number, String extension)
        {
            String beautifulNumber = "";
            if (!String.IsNullOrEmpty(number))
            {
                beautifulNumber = "(" + number.Substring(0, 3) + ") " +
                                         number.Substring(3, 3) + "-" +
                                         number.Substring(6, 4);
            }
                if (!String.IsNullOrEmpty(extension))
                {
                    beautifulNumber += " x" + extension;
                }

            return beautifulNumber;
        }


这就是我在类本身中具有变量的方式。

private string _PhonePrimary;
[DisplayName("Phone Primary")]
public string PhonePrimary
{
    get
    {
        if(this._PhonePrimary != null)
        {
             this._PhonePrimary = beautifyPhoneNumber(this._PhonePrimary, this.Extension);
        }
        return this._PhonePrimary;
    }
    set
    {
        this._PhonePrimary = value;
    }
}


在大多数情况下,这都可以正常工作。如果数据库中该记录有扩展名,则这些数字将以“(727)988-4545”或“(727)988-4545 x12”的形式输出到屏幕。

当我执行HttpPost请求时出现问题。发布请求中的模型信息如下所示。

_PhonePrimary =“(727)988-4545”

PhonePrimary =“((((7)2)-7)-”

最佳答案

如前所述,您似乎在对已经美化的号码拨打beautifyPhoneNumber。

这是一个使用正则表达式的实现,可以帮助您入门:

public static String BeautifyPhoneNumber(string numberToBeautify)
    {
        //The below gives us capture groups for each
        //individual piece of the number.
        var regularExpression = new Regex(@"(\d{3})(\d{3})(\d{4})(x\d*)?");
        //This matches a number that's already been beautified,
        //so we can guard against beautifying twice.
        var alreadyBeautifulExpression = new Regex(@"(\(\d{3}\)) (\d{3})-(\d{4}) ?(x\d*)?");
        var beautifulNumber = string.Empty;
        var separator = "-";
        var space = " ";
        //This prevents us from accidentally beautifying
        //something more than once
        //You could also guard against this in your getter using a
        //IsBeautified extension, using the alreadyBeautifulExpression above
        if (alreadyBeautifulExpression.IsMatch(numberToBeautify))
        {
            return numberToBeautify;
        }

        //Trying to protect against invalid input... May be insufficient,
        //Or unnecessary
        if (string.IsNullOrEmpty(numberToBeautify)
            || regularExpression.Matches(numberToBeautify).Count <= 0)
        {
            return beautifulNumber;
        }

        GroupCollection groups = regularExpression.Matches(
                                                numberToBeautify)[0].Groups;

        //More protection against invalid input
        if (groups.Count < 3)
        {
            return beautifulNumber;
        }

        //Given "7689131234",
        beautifulNumber += "(" + groups[1] + ")" + space; //gives us "(768) "
        beautifulNumber += groups[2] + separator; //gives us "(768) 913-"
        beautifulNumber += groups[3]; //gives us "(768) 913-1234"
        //If we have an extension, we add it.
        if (groups[4] != null)
        {
            beautifulNumber += space + groups[4];
        }

        return beautifulNumber;
    }


给出以下输入:
    7279884545
    7279884545x12
    (727) 988-4545

返回:
     (727) 988-4545
     (727) 988-4545 x12
     (727) 988-4545

关于c# - 设置电话号码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22873258/

10-10 13:42