【问题标题】:C# annoyance - access data outside of stingC# 烦恼 - 访问 sting 之外的数据
【发布时间】:2013-03-15 12:02:50
【问题描述】:

我用 C# 做了一个电子邮件验证程序,但是如何检查字符串之外的数据?

这是我的 C# 代码:

private bool CheckEmail()
{
    string email1 = email.Text;

    //calculating the length of the email
    int EmailLen = email1.Length;
    int num = 0;

    //the first character of the email must not be the "@"
    if (email1.Substring(0, 1) != "@")
    {
        //checking the email entered after the first character as it is not a "@" so i will start from 1.
        for (int i = 1; i < EmailLen; i++)
        {
            //prevents there from being two "@"next to each other
            if (email1[i] == '@' && (i + 1) < email1.Length && email1[i + 1] != '@')
            {
                //if there is an "@" in the email then num will increase by one
                num = num + 1;

                //now the stored value of i is the position where the "@" is placed. j will be i+2 as there should be at least one character after the "@"
                int j = i + 2;
                if (j < EmailLen)
                {
                    for (int k = j; k < EmailLen; k++)
                    {
                        //when it finds a "." In the email, the character after the "." Should not be empty or have a space, e.g. it should be something like ".com"

                        if (email1[k] == '.' && k + 1 < email1.Length && email1[k + 1] != ' ')
                        {
                            num = num + 1;
                        }
                    }
                }
                else
                {
                    break;
                }
            }
        }
    }
    else
    {
        num = 0;
    }

    //if the num is 2, then the email is valid, otherwise it is invalid.  If the email had more than one "@" for example, the num will be greater than 2.

    if (num == 2)
    {
        return true;
    }
    else
    {
        return false;
    }
}

当我尝试输入“aa@”时,我收到此错误:“索引和长度必须引用字符串中的位置。”

当我输入 aa@a 时。 ,我收到此错误:“索引和长度必须引用字符串中的位置。”

【问题讨论】:

    标签: c# visual-studio-2010 if-statement for-loop substring


    【解决方案1】:

    您不能访问字符串之外的数据。这是有充分理由的 - 这样做会违反类型安全性,而类型安全性是 .NET CLR 等虚拟机的主要吸引力。

    你只是想检查你的界限以确保你没有试图访问不存在的字符串的一部分。顺便说一句,对于检查单个字符,您完全希望使用 email1[i],而不是 email1.Substring(i, 1),因此您不会在左、右和中心构造新的字符串对象。

    你的第一个测试应该是:

    if (email1[i] == '@' && i + 1 < email1.Length && email1[i + 1] != '@')
    

    【讨论】:

    • 第一次测试有效,你将如何转换这一行:email1.Substring(k, 1) == "." && email1.Substring(k + 1, 1) != "" && email1.Substring(k + 1, 1) != " "
    • 我可能会改写第二句话:原因是字符串之外没有数据,就你和字符串而言。虽然后面可能有内存,但通过字符串查看其内容与您无关。实际上,字符串缓冲区之后的内存很有可能甚至不属于您,并且尝试访问它会使您的程序崩溃。
    • @Hamoudy:您在此处提供的代码甚至无法验证字符串是否包含良好的电子邮件地址。从我的脑海中只挑一个,abc"def"ghi@jkl.com 不是一个有效的电子邮件地址,但你的程序允许它。
    • @Hamoudy:在编写更多代码之前,您应该阅读相关文档。一个好的起点是tools.ietf.org/html/rfc3696
    • 非常感谢您的建议,MattW 您有很多优点!
    【解决方案2】:

    你的问题是

    email1.Substring(i + 1, 1)
    

    在 for 循环的最后一次迭代中,i == EmailLen -1。

    所以 i + 1 == EmailLen,它是字符串末尾的一个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      • 2014-01-27
      相关资源
      最近更新 更多