A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 郑志强 中级黑马   /  2013-3-14 15:01  /  1737 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 郑志强 于 2013-3-14 19:02 编辑

用控制台怎么写?

判断 是否合法。返回BOOL值。   没思路呀。

不用正则表达式

7 个回复

倒序浏览
你可以把验证EMAIL是否正确的代码写成一个方法  返回值是bool类型。
如果验证的email 是正确的 那么你可以返回一个bool值 为true
判断emal是否合法,可以调用string类的.split()方法 分割字符串 。
很简单的,你可以试一下。
回复 使用道具 举报
下面是我的思路,新手.

public static bool IsEmail (string email)
{
    if(!email.Contains('@'))
    {
        return false;
    }

    //根据@分割字符串

    string[] strs = email.Split(new char[]{'@'},SplitOption.RemoveNull);

    //判断数组长度

    if(strs.Length>2)
    {
        return false;
    }

    //接下来只要判断@后面的东西即可
    string str = strs[1];
  .....................................
    //
}
回复 使用道具 举报
{:soso_e101:}何必不用正则表达式呢- -明明可以很方便的。。。
下面的是正则表达式的做法。。。
  1.         static void Main(string[] args)
  2.         {
  3.             Console.WriteLine("请输入邮箱:");
  4.             string email = Console.ReadLine();
  5.             if (Regex.IsMatch(email, @"/(\S)+[@]{1}(\S)+[.]{1}(\w)+/"))//如果输入的符合该正则表达式则输出你的邮箱是对的
  6.             {
  7.                 Console.WriteLine("恭喜您!你懂得!");
  8.             }
  9.             else
  10.             {
  11.                 Console.WriteLine("您输入的邮箱错了- -");//如果输入的是错的,就提示错了呗
  12.             }
  13.             Console.ReadKey();
  14.         }
复制代码
如果你不用正则表达式,就要用indexof来判断里面是否有"@",是否有"."等符号,然后"@"要在"."的前面,就可以简单的判断是否正确,只是简单的判断0.0
回复 使用道具 举报
又写了个灰常简单的- -不用正则表达式的方法。。。
  1.         static void Main(string[] args)
  2.         {
  3.             Console.WriteLine("Please input your e-mail,I will check it :");
  4.             string mail = Console.ReadLine();
  5.             if (mail.IndexOf("@") > 0)//indexof的作用是查找字串中指定字符或字串首次出现的位置,返首索引值。
  6.             {
  7.                 if (mail.IndexOf(".") > mail.IndexOf("@"))//如果"."出现在"@"的后面,那么就对啦~
  8.                 {
  9.                     Console.WriteLine("RIGHT!");
  10.                 }
  11.             }
  12.             else
  13.             {
  14.                 Console.WriteLine("WRONG!");
  15.             }
  16.             Console.ReadKey();
  17.         }
复制代码
希望能帮到你
回复 使用道具 举报
额 我自己做出来了
  1. Console.WriteLine("输入Email");
  2.             string email = Console.ReadLine();
  3.             //判断是否含有@和.的
  4.             if (email.Contains("@") && email.Contains("."))
  5.             {
  6.                 //判断是不是@或者.开头或者结尾
  7.                 if (!(email.StartsWith("@")) && !(email.EndsWith("@")) && !(email.StartsWith(".")) && !(email.EndsWith(".")))
  8.                 {
  9.                     //把email转换为字符串组
  10.                     char[] values = email.ToCharArray();
  11.                     string value = new string(values);
  12.                     //判断.是否在@的后面.并且@的位置不和.相连
  13.                     if (value.IndexOf('@') < value.IndexOf('.')&&!(value.IndexOf('@')+1==value.IndexOf('.')))
  14.                     {
  15.                         Console.WriteLine("email地址合法");
  16.                         Console.ReadKey();
  17.                         return;
  18.                     }
  19.                     else
  20.                     {
  21.                         Console.WriteLine("email地址不合法");
  22.                         Console.ReadKey();
  23.                         return;
  24.                         
  25.                     }
  26.                 }
  27.                 else
  28.                 {
  29.                     Console.WriteLine("email地址不合法");
  30.                 }
  31.             }
  32.             else
  33.             {
  34.                 Console.WriteLine("email地址不合法");
  35.             }
  36.             Console.ReadKey();
复制代码
回复 使用道具 举报
{:soso_e127:}为啥不用正则啊,正则是不是只能判断格式啊,比如有173.com?可是没有173邮箱啊
回复 使用道具 举报
新手,学习了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马