从Email中提取出用户名和域名:xxx@126.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication8
{
class Program
{
static void Main(string[] args)
{
// 从Email中提取出用户名和域名:xxx@126.com
// IndexOf找到@的位置。SubString
// 要考虑Email地址错误的情况
string email = Console.ReadLine();
if (!email.Contains("@") || email.StartsWith("@") || email.EndsWith("@"))
{
Console.WriteLine("Email地址格式不正确");
}
else {
int atIndex = email.IndexOf("@");
string username = email.Substring(0,atIndex);
string domain = email.Substring(atIndex+1);
Console.WriteLine("用户名:{0}\n域名:{1}",username,domain);
return;
}
}
}
}