c#判断email地址是否为合法
时间:2020-11-30 13:41:29|栏目:.NET代码|点击: 次
题目要求:
关键代码如下所示:
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(); } }