首页 > 解决方案 > 在 C# 中,如何查看帐户中的第一个字符是否为特定值?

问题描述

我有一个包含 15 位帐号的字符串。第一个数字不能是 0、1、8 或 9。我想说的是:

public bool ValidateAccountNumber(string Accountnumber )
{
    char[] Invalidboro = new char[] { '0', '1', '8', '9'};
    bool returnValue = true;

    if (Accountnumber.Length != 15 || Accountnumber.Substring(1, 1).Contains(Invalidboro))
    {
        returnValue = false;
    }
    return returnValue;
}

我是 C# 新手,不知道该怎么做。

标签: c#contains

解决方案


正则表达式

public bool ValidateAccountNumber(string Accountnumber)
{
    return Accountnumber != null && Regex.IsMatch(Accountnumber, "^[2-7][0-9]{14}$");
}

推荐阅读