C/C++/C# 字串中 計算/統計 特定字串出現次數 [How would you count the occurrences of a string within a string?]
C/C++/C# 字串中 計算/統計 特定字串出現次數 [How would you count the occurrences of a string within a string?]
資料來源: http://jengting.blogspot.com/2014/06/c_16.html
C# Code:
namespace StringCount
{
class Program
{
static void Main(string[] args)
{
string strData = "Hello, how are youyou you you you, you you yy?";
string goal = "you";
string strReplace = strData.Replace(goal, "");//字串替換
int times = (strData.Length - strReplace.Length) / goal.Length ;// 長度差/尋長字串長度
string message = string.Format("字串 {0} 在字串 {1} 中,出現 {2} 次", goal, strData, times);
Console.WriteLine(message);
}
}
}