C#计算字符串中子串出现次数的另类方法

转自http://www.zu14.cn/2011/04/13/donet-csharp-calculate-sub-string-counts-of-string/

/// <summary>
/// 计算字符串中子串出现的次数
/// </summary>
/// <param name="str">字符串</param>
/// <param name="substring">子串</param>
/// <returns>出现的次数</returns>
static int SubstringCount(string str, string substring)
{
if (str.Contains(substring))
{
string strReplaced = str.Replace(substring, "");
return (str.Length - strReplaced.Length) / substring.Length;
}

return 0;
}

  

posted on 2011-08-12 17:00  大松  阅读(620)  评论(0编辑  收藏  举报