碧水寒潭

追寻平淡的幸福:和喜欢的人在一起,做自己喜欢的事……
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

字符串中跟指定字符集中的字符匹配的个数

Posted on 2011-06-17 13:53  碧水寒潭  阅读(431)  评论(0编辑  收藏  举报

例:给定字符串 str = '每周一、三、五、日 13:00-15:00'

  匹配字符集 arr = { '一','二','三','四','五','六','日'}

问题:返回字符串 str 中包含多少个跟字符集 arr 匹配的字符?

解决:

Code
1 /// <summary>
2 /// 返回字符串中跟指定字符集中的字符匹配的个数
3 /// </summary>
4 /// <param name="str"></param>
5 /// <returns></returns>
6 public static int charCount(string str)
7 {
8 int count = str.Length;
9 str = Regex.Replace(str, @"一|二|三|四|五|六|日", "");
10 count = count - str.Length;
11 return count;
12 }