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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 指尖舞者 中级黑马   /  2014-6-30 08:26  /  1780 人查看  /  16 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 指尖舞者 于 2014-7-2 15:01 编辑

Win Form中最基础的窗口问题                  
  1. string input = textBox1.Text;
  2. string[] strs = input.Split('@'); /*改方法作用是以某个字符为分隔,
  3. 构成一个数组的元素*/
  4. if (strs.Length != 2) //??如果用户名中含有@怎么办?
  5. {
  6. MessageBox.Show("输入的Email地址格式非法");
  7. return;
  8. }
  9. textBox2.Text = strs[0];
  10. textBox3.Text = strs[1];
复制代码



问题就是如果该用户的用户名中有@,那么input.Split('@');如何选择@?   

查证了Email用户名不能包含@字符  汗!


评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

16 个回复

倒序浏览
你的问题有点绕。。   string[] strs = input.Split('@');   我看你的程序就是要使用string.split对字符串按你自己规定的“@”符进行分割然后放到strs数组中啊!!    你的意思是问Split('@')具体怎么做的是吗?   例如:1234566@qq.com  通过string[] strs = input.Split('@');后就可以将其分为1234566和qq.com两部分存在数组strs中,so  strs中就相当于有两个元素值str[0]=1234566,str[1]=qq.com  希望可以帮到你!

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
dugubaitian 发表于 2014-6-30 09:22
你的问题有点绕。。   string[] strs = input.Split('@');   我看你的程序就是要使用string.split对字符串 ...

是的 我知道它如何分割的 我意思 加入我们自己的用户名里面包含了@字符  在用这个方法来分割 应该就有BUG了    有没有别的办法呢
回复 使用道具 举报
指尖舞者 发表于 2014-6-30 09:27
是的 我知道它如何分割的 我意思 加入我们自己的用户名里面包含了@字符  在用这个方法来分割 应该就有BUG ...

Email地址只能由字母数字下划线组成,就像你的if判断,如果分割的不是两个,那就是输入的错误
回复 使用道具 举报
从Email中提取出用户名和域名:abc@163.com
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入你的Email:");
            string s = Console.ReadLine();
            int atm = s.IndexOf('@');                   //查出@的位置(定位)
            string name = s.Substring(0, atm);          //从开始截取到@之间的字符串,两个参数从第几位到第几位截取字符串.
            string yuming = s.Substring(atm + 1);       //括号里一个参数表示从第(atm + 1)位开始到末尾截取字符串.
            Console.WriteLine("用户名是:{0},域名是:{1}", name, yuming);
            Console.ReadKey();
        }
    }
}

//  int atm = s.IndexOf('@');                   //查出@的位置(定位)
使用上面这句查找@位置  

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
本帖最后由 指尖舞者 于 2014-6-30 12:39 编辑
天佑の清清 发表于 2014-6-30 11:20
从Email中提取出用户名和域名:。
    class Program
    {


t atm = s.IndexOf('@');   这个函数貌似不错 回去试试  

但是 如果出现2个@怎么解决呢?

比如[url=mailto:heima@luntan@126.com]heima@luntan@126.com[/url]    我们知道用户名是heima@luntan  域名是126.com 但是好像问题还在

点评

楼主你帖子上那个复制代码功能怎么弄出来的?  发表于 2014-6-30 19:06

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
涨知识了!
回复 使用道具 举报
建议楼主获取最后一个"@"的位置的返回值,如果不等于1,那个就用substring进行分割,如果等于1就直接用split进行分割,一般用户名不会出现特殊字符,输入的时候直接给注释掉了!

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
本帖最后由 The_Enternal 于 2014-7-12 17:30 编辑


  1. <div><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">using System;</span></div><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">using System.Collections.Generic;</span><span style="background-color: rgb(247, 247, 247);">
  2. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">using System.Text;</span><span style="background-color: rgb(247, 247, 247);">

  3. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">namespace 邮箱地址</span><span style="background-color: rgb(247, 247, 247);">
  4. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">{</span><span style="background-color: rgb(247, 247, 247);">
  5. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">    class Program</span><span style="background-color: rgb(247, 247, 247);">
  6. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">    {</span><span style="background-color: rgb(247, 247, 247);">
  7. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">        static void Main(string[] args)</span><span style="background-color: rgb(247, 247, 247);">
  8. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">        {</span><span style="background-color: rgb(247, 247, 247);">
  9. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            Console.WriteLine("请输入邮箱地址:");</span><span style="background-color: rgb(247, 247, 247);">

  10. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            string mail = Console.ReadLine();</span><span style="background-color: rgb(247, 247, 247);">

  11. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            string userName, domainName;</span><span style="background-color: rgb(247, 247, 247);">

  12. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            //判断用户名中是否含有"@"字符</span><span style="background-color: rgb(247, 247, 247);">
  13. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            if (mail.LastIndexOf("@") !=mail.IndexOf("@"))</span><span style="background-color: rgb(247, 247, 247);">
  14. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            {</span><span style="background-color: rgb(247, 247, 247);">
  15. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">                //字符串中"@"的第一个索引和最后一个索引值不相等,则表示含有两个"@"字符</span><span style="background-color: rgb(247, 247, 247);">
  16. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">                //从开头到最后一个"@"字符以前的字符串都是用户名</span><span style="background-color: rgb(247, 247, 247);">
  17. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">                userName = mail.Substring(0, mail.LastIndexOf("@"));</span><span style="background-color: rgb(247, 247, 247);">
  18. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">                //最后一个"@"字符往后都是域名</span><span style="background-color: rgb(247, 247, 247);">
  19. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">                domainName = mail.Substring(mail.LastIndexOf("@") + 1);</span><span style="background-color: rgb(247, 247, 247);">
  20. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            }</span><span style="background-color: rgb(247, 247, 247);">

  21. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            else</span><span style="background-color: rgb(247, 247, 247);">
  22. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            {</span><span style="background-color: rgb(247, 247, 247);">
  23. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">                //只含有一个"@"字符,直接从"@"字符的索引处分割</span><span style="background-color: rgb(247, 247, 247);">
  24. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">                userName = mail.Substring(0, mail.IndexOf("@"));</span><span style="background-color: rgb(247, 247, 247);">

  25. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">                domainName = mail.Substring(mail.IndexOf("@") + 1);</span><span style="background-color: rgb(247, 247, 247);">
  26. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            }</span><span style="background-color: rgb(247, 247, 247);">

  27. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            Console.WriteLine("——————————————————————");</span><span style="background-color: rgb(247, 247, 247);">
  28. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            Console.WriteLine("用户名:{0},域名:{1}",userName,domainName);</span><span style="background-color: rgb(247, 247, 247);">

  29. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">            Console.ReadKey();</span><span style="background-color: rgb(247, 247, 247);">
  30. </span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">        }</span><div><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">  }</span></div><div><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">}</span><span style="line-height: 1.5; background-color: rgb(247, 247, 247);">
  31. </span></div>
复制代码




评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
你好,当有人给你回复后,请及时把帖子编辑为提问结束,这样版主才给你加技术分
回复 使用道具 举报
回复The_Enternal 的问题,由于不能发消息,只能这里回复

发帖子的时候有个 <> 图标 点击就可以粘贴进去代码了:)
回复 使用道具 举报
专属第三 发表于 2014-6-30 11:07
Email地址只能由字母数字下划线组成,就像你的if判断,如果分割的不是两个,那就是输入的错误 ...

额 好吧 我查了下 邮箱用户名还真不能包含@  我勒个去
回复 使用道具 举报
本帖最后由 指尖舞者 于 2014-7-1 10:09 编辑
The_Enternal 发表于 2014-6-30 19:03
using System;
using System.Collections.Generic;
using System.Text;


刚刚发现 你在这回复帖子了  你这代码还真是用心写了的

你这2个查找@位置的方法不错 记下了

那个直接放代码 你点击有个<>图标 粘贴进去就可以了

1.jpg (6.21 KB, 下载次数: 59)

1.jpg
回复 使用道具 举报
你可以使用正则表达式进行校验啊
回复 使用道具 举报
追梦无悔 发表于 2014-7-1 10:08
你可以使用正则表达式进行校验啊

额  这个不会 零基础 基本是那个只会视频教学里面的
回复 使用道具 举报
原来如此,非常感谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马