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