从richtextbox中删除单词的最佳方法是什么?

时间:2013-08-21 16:50:43

标签: c# winforms richtextbox

* 强文 我有一个richTextBox,喜欢搜索以Hello Worlds开头并包含“HERE”的句子行。结束;我想删除半列 “HERE”。 。我的句子,下面的例子只是一行句子,但句子可能是2行长,所以条件应该从Hello Worlds开始并以;半列除了删除 “HERE”。*

我的一句话:

Hello Worlds the weather is too hot "HERE"."IN" CANADA!;

我的2行句子可能就是这样:

Hello Worlds the weather is too hot "HERE"."IN" CANADA!. But we are still like it;

结果应为一行:

 Hello Worlds the weather is too hot "IN" CANADA!;

结果应为2行:

Hello Worlds the weather is too hot "IN" CANADA!. But we are still like it;

我坚持使用我的代码:

                   List<string> rt = new List<string>();
                        foreach (string line in richTextBox1.Lines)
                        {
                            if (line.StartsWith("Hello Worlds") && line.Contains("HERE"))
                            {
                               //remove "HERE".
                            }
                        }

1 个答案:

答案 0 :(得分:1)

你可以这样做

string[] lines = richTextBox1.Lines;
List<string> linesToAdd = new List<string>();
string filterString = "\"HERE\".";
foreach (string s in lines)
{
    string temp = s;
    if (s.StartsWith("Hello Worlds") && s.EndsWith(";") && s.Contains(filterString))
       temp = s.Replace(filterString, string.Empty);
    linesToAdd.Add(temp);
 }
 richTextBox1.Lines = linesToAdd.ToArray();