隔壁司机老王

微博:i王YOU

导航

c#判断是否为合法的email地址

题目要求:

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入正确的邮箱地址,以 @sina.com 结尾");
            string us =Convert.ToString(Console.ReadLine());

            string str1 = us.Replace("@", "");
            int count = us.Length - str1.Length;

            string str2 = us.Replace(".", "");
            int count2 = us.Length - str2.Length;
            int count3 = us.Length - 1;
            int xl=us.IndexOf(".")-us.IndexOf("@");
            bool mw = (us.IndexOf("@sina.com") == (us.Length-9));

            while (count == 1 && count2 == 1 && us.IndexOf("@") > 0 && us.IndexOf(".") > us.IndexOf("@") 
                && us.IndexOf(".")<count3)
            {
                if (xl != 1 && us.Contains("@sina.com") && mw)
                {
                    Console.WriteLine("正确");
                }
                else {

                    Console.WriteLine("输入的邮箱地址有误");
                }

                break;
            }
            Console.ReadLine();
        }
    }
2016 - 07 - 20

posted on 2016-07-20 10:35  隔壁司机老王  阅读(1101)  评论(1编辑  收藏  举报