且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何查找字符串中某个特定句子的所有出现?

更新时间:2022-11-07 23:34:14

这是一种非LINQ的方法:

Here's a non-LINQ method of doing it:

string str = "Today is friday! I'm am having trouble programming this. Today is friday! Tomorrow is saturday. Today is friday!";

StringBuilder sb = new StringBuilder();
int index = 0;
do
{
    index = str.IndexOf("Today is friday!", index);
    if (index != -1)
    {
        sb.Append("Today is friday!");
        index++;
    }
} while (index != -1);

string repeats = sb.ToString();