新手学习 C#控制台程序_邮箱地址验证
各位:
最进在学C#,从基础部分学起,现在遇到一个问题怎么都搞不了,请给位帮忙指点一下:最好能给出具体的代码
一、输入一个邮箱地址,然后将这个邮箱的用户名和域名分别输出
我现在已经可以正常输出用户名和域名,我就想在输入邮箱地址的时候做一个验证:验证所输入的数据是否格式正确(xxxx@xxx.com);想过用正则表达式,但我自己看的不是很明白,所以也用不了;另外一个就是在程序里面检测是否存在"@"和".com"字符串,但不知道怎么去验证。现在想请给位大大指点一下
具体程序代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions; namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string email;
int a;
Console.WriteLine("请输入一个邮箱地址:");
email = Convert.ToString(Console.ReadLine());
//想在这里加一个输入验证是否为正确的邮箱地址,如果是则正常运行,否则转回到输入提示那里
a = email.IndexOf("@");
string val1 = email.Substring(0, a);
string val2 = email.Substring(a+1, email.Length-a-1);
Console.WriteLine("所输入邮箱的用户名为:{0},域名为:{1}",val1,val2);
}
}
}