//验证字符串切割函数Split() string st = "abcd erfas dflkj "; char[] a = new char[] { ' ' }; //当引号内空一个space string[] b = st.Split(a); //string[] b = st.Split(new char[] { ' ' }); foreach (string i in b) { Console.WriteLine("{0}",i); } string words = "攻击距离10米,对目标造成50%的法术伤害,额外造成80%火元素伤害"; string[] split = words.Split(new char[] { ',' }); //以逗号分隔字符串 foreach (string s in split) { Console.WriteLine(s); } string st1 = "您2要验证的字符串1,]w!"; byte[] l = Encoding.UTF8.GetBytes(st1); //字节长度 string str = System.Text.RegularExpressions.Regex.Replace(st, @"[^\w]|_", "");//替换掉所有的符号,如逗号,感叹号等 string[] x = System.Text.RegularExpressions.Regex.Split(str, @"\d");// 数字个数 string[] xx = System.Text.RegularExpressions.Regex.Split(str, @"[\u4e00-\u9fa5]"); //汉字的个数 string[] xxx = System.Text.RegularExpressions.Regex.Split(str, @"[a-zA-Z]"); //字母的个数 Console.WriteLine("字节长度:" + (l.Length - 1) + "\r\n 数字个数:" + (x.Length - 1) + "\r\n 汉字的个数:" + (xx.Length - 1) + "\r\n 字母的个数:" + (xxx.Length - 1)); Console.WriteLine(str); string str1 = "[dfasd][afasdf][adfaf]"; Console.WriteLine(Regex.Matches(str1, "]").Count); //匹配特定字符个数,如计算"]"的个数为3个
using System.Text.RegularExpressions;
判断字符串里含有非数字元素
string input_num = "11w13"; //方法一,使用正则表达 if (Regex.IsMatch(input_num, "^((\\+|-)\\d)?\\d*$")) { Console.WriteLine("all is number!"); } else { Console.WriteLine("number & other charactor!"); } //方法二,用char的IsNumber for (int i = 0; i < input_num.Length; i++) { if (char.IsNumber(input_num[i])) { Console.WriteLine("shuzi"); } else { Console.WriteLine("feizhuzi"); } }