在asp.net中统计字符串A在字符串B中出现的次数

思路有多种。

  1. 正则验证匹配
  2. split分割
  3. replace替换算长度

下面以第三种方法举例

            string strReplace = "http";
            int CountHttp = ((strContent.Length – (strContent.Replace(strReplace, string.Empty)).Length)) / strReplace.Length;

思路有多种。

  1. 正则验证匹配
  2. split分割
  3. replace替换算长度

下面以第三种方法举例

            string strReplace = "http";
            int CountHttp = ((strContent.Length – (strContent.Replace(strReplace, string.Empty)).Length)) / strReplace.Length;
 

实现的思路,想算A字符串在B字符串出现的次数.

可以用 (B字符串的长度, 去除含有的A字符串后的B字符串的长度) 除以A字符串的长度。

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *