问题
*strong textI have a richTextBox and like to search the line of sentence that starts with Hello Worlds and Contains "HERE". and end with ; semi-column than I like to delete only "HERE". . My sentence, below example is only one line of sentence, but sentence may be 2 lines long, so condition should be start with Hello Worlds and ends with ; semi-column than remove "HERE".*
my one line of sentence:
Hello Worlds the weather is too hot "HERE"."IN" CANADA!;
my 2 lines of sentence might be this:
Hello Worlds the weather is too hot "HERE"."IN" CANADA!. But we are still like it;
result should be for one line:
Hello Worlds the weather is too hot "IN" CANADA!;
result should be for 2 lines:
Hello Worlds the weather is too hot "IN" CANADA!. But we are still like it;
well I stuck on my code:
List<string> rt = new List<string>();
foreach (string line in richTextBox1.Lines)
{
if (line.StartsWith("Hello Worlds") && line.Contains("HERE"))
{
//remove "HERE".
}
}
回答1:
you can do this
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();
来源:https://stackoverflow.com/questions/18363308/what-is-the-best-way-to-remove-words-from-richtextbox