2014-04-02 43 views
2

我怎样才能从RichTextBox一“串”删除单词。如何删除所有比赛和删除文本

例子:

[02/04/2014 17:04:21] Thread 1 Banned: [email protected] 
[02/04/2014 17:04:21] Thread 2: Banned: [email protected] 
[02/04/2014 17:04:21] Thread 3: Banned: [email protected] 
[02/04/2014 17:04:21] Thread 4: Banned: [email protected] 

我想与行“禁止”这个词来删除所有行。

我该怎么做?

在此先感谢。

+1

使用正则表达式和捕获。 –

回答

4

您可以使用LINQ删除包含工作的所有行“禁止”:

richTextBox1.Lines = richTextBox1.Lines 
    .Where((line, b) => !line.Contains("Banned")) 
    .Select((line, b) => line).ToArray(); 
+1

非常感谢,非常感谢。 – Syahmie

+0

@Syahmie不客气:) –

2

你可以尝试使用回答这个职位 - 我会稍微调整一下代码neaten它一点。

URL:从URL what is the best way to remove words from richtextbox?

代码段(这将需要进行一些清理工作)。

string[] lines = richTextBox1.Lines; 
List<string> linesToAdd = new List<string>(); 
string filterString = "Banned"."; 
foreach (string s in lines) 
{ 
    string temp = s; 
    if (s.Contains(filterString)) 
     temp = s.Replace(filterString, string.Empty); 
    linesToAdd.Add(temp); 
} 
richTextBox1.Lines = linesToAdd.ToArray(); 

我会调整上述代码,并同时仍使用循环,只是检查是否行包含您寻找“禁止”这个词,然后删除行/做什么是需要它。

我希望这可以帮助?

+0

Hello Worlds?在发布前做好。 – kpull1

+0

@ kpull1这不是一个完整的解决方案,正如我在答案中提到的那样...他可以使用类似的东西,但是(我说这个)“代码需要被编辑”... – Hexie

+0

@ kpull1我已经做了在答案的调整中,无论我还是不觉得这值得一一-1 – Hexie

2

我知道这个方法看起来丑陋。但是,如果您不想从richtextbox中的现有文本中移除格式,那么您应该使用此方法。这个例子没有经过测试,但是,你可以从这里得到逻辑。

for (int iLine = 0; iLine < rtf.Lines.Length; iLine++) 
{ 
    if (rtf.Lines[iLine].Contains("Banned")) 
    { 
     int iIndex = rtf.Text.IndexOf(rtf.Lines[iLine]); 
     rtf.SelectionStart = iIndex; 
     rtf.SelectionLength = rtf.Lines[iLine].Length; 
     rtf.SelectedText = string.Empty; 
     iLine--; //-- is beacause you are removing a line from the Lines array. 
    } 
} 
+1

这也是工作..但我更喜欢选择Guilherme ..无论如何感谢。 – Syahmie

+1

是的,Guilherme的解决方案相当简单快捷。 – Shell