c# - 如何找到字符串中特定句子的所有出现?

标签 c# regex linq substring

假设我有这样一个字符串:

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

我想搜索这个字符串,抓取所有说“今天是星期五!”的句子,然后用我刚找到的句子创建一个新字符串。

上述字符串的预期结果是:

string output = "Today is friday!Today is friday!Today is friday!"

编辑:LINQ 不是强制性的。

谢谢!

最佳答案

这是一种非 LINQ 方法:

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();

关于c# - 如何找到字符串中特定句子的所有出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12220617/

相关文章:

c# - 我需要帮助弄清楚这种FFT方法

regex - RegEx用于在MATLAB中捕获科学数字

c# - Linq 相当于 SQL LIKE [a-f]

C# Linq 获取后代的值

C# STAThread COMException 异常

c# - 为什么向字符串添加换行符会在 Interop.Word 中产生错误?

php - 如何仅在引用的子字符串中用点替换逗号?

javascript - 用于使用环视删除空行的正则表达式

c# - Linq-to-Sql:递归获取 child

java - 使用类路径 (cp) 在单独的位置执行 java 控制台应用程序