字符串是这样的:
string str="key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47, key=0Sr4C, age=68, key=CGEqo, age=76, key=IxKVQ, age=79, key=eD221, age=29, key=XZbHV, age=32, key=k1SN5"
Question:需要找出该字符串中对少个超过50的数据。
法1:正则
int count = 0;
foreach (var item in Regex.Matches(str, @"age=\d+"))//取出所有以age=开头,后面是n位数字得子串
{
int age = int.Parse(Regex.Match(item.ToString(), @"\d+").Value);//在取出对应的n位的数字
if (age > 50) count++;
}
Console.WriteLine(count);
法2:思路先把字符串用 ‘,’ 切分为数组,取出所有的偶数项,在用 ‘=’ 切分,取出下标为[1]的即为年龄