首页 新闻 赞助 找找看

c# 字符串的遍历

0
[待解决问题]

例子:在textbox中输入一段字符串“klkLOIYG098=-‘;”,把每个字符按大写字母,小写字母,数字,和其他字符进行分类显示。单击按钮后,在textbox中显示大写字母:LOIYG小写字母:klkl数字:098其他字符:=-’;大概就是这样了,一定在form中实现哦

915545924的主页 915545924 | 菜鸟二级 | 园豆:204
提问于:2013-05-16 17:03
< >
分享
所有回答(2)
1
 1 string str=textbox.text;
 2 char[] charList = str.ToArray();
 3 string bNum="";
 4 string sNum ="";
 5 string nNum = "";
 6 string otherNum = "";
 7               foreach (char c in charList)
 8               {
 9                   if (System.Text.RegularExpressions.Regex.IsMatch(c.ToString(), "[A-Z]"))
10                   {
11                       bNum += c.ToString();
12                   }
13                   else if (System.Text.RegularExpressions.Regex.IsMatch(c.ToString(), "[0-9]"))
14                   {
15                       nNum += c.ToString();
16                   }
17                   else if (System.Text.RegularExpressions.Regex.IsMatch(c.ToString(), "[a-z]"))
18                   {
19                       sNum += c.ToString();
20                   }
21                   else
22                   {
23                       otherNum += c.ToString();
24                   }
25               }
26               Console.WriteLine("bNum:" + bNum);
27               Console.WriteLine("sNum:" + sNum);
28               Console.WriteLine("nNum:" + nNum);
29               Console.WriteLine("otherNum:" + otherNum);
30               Console.ReadLine();

 

 

托雷 | 园豆:184 (初学一级) | 2013-05-16 17:42
1

用正则吧

W宁宁 | 园豆:522 (小虾三级) | 2013-05-16 18:39
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册