计算某个字符在字符串中的个数

 

 1 private int SubstringCount(string str, string a)
 2 {
 3     int count = 0;
 4     if (str.Contains(a))
 5     {
 6         string s = str.Replace(a, "");
 7         count = str.Length - s.Length;
 8     }
 9     return count;
10 }    

  因为替换的是空格,所以必须用字符串替换。

如果用字符替换,编译不通过,会报错。提现替换的为 空字符 。

posted @ 2020-11-24 10:31  llkj  阅读(319)  评论(0编辑  收藏  举报