I have checkboxlistwhere the user selects which lines he will remove from richtextboxwith a text printout. For example, a listing header with a company name, date, etc.
These unwanted lines are changed from any other text file, so the user needs to choose through checkboxwhich lines will be deleted.
For example, a line is PAGE 1 OF 12repeated on every page, but, of course, the page changes. So, there is a substring to get only the beginning of a sequence, so it can delete all lines starting with PAGE. The printed date begins with the word DATEand must also be deleted.
But this is not an important part. The important part is that the number of lines to be eliminated varies for each file. Sometimes he will not have a company name; sometimes it will be. I ran a loop FOR, going through each checked line that I need to delete, and deleting everything from richtextbox. Then she returned to the beginning and repeated the process of the FOREACHmarked element. It takes a lot of time.
So, I came out with this solution, using two textBoxfor this example:
string searchKeyword = textBox1.Text;
string searchKeyword2 = textBox2.Text;
var lines = richTextBox1.Lines;
foreach (string line in lines.Where(l => (!l.StartsWith(searchKeyword)) && (!l.StartsWith(searchKeyword2))))
_items.Add(line);
listBox2.DataSource = null;
listBox2.DataSource = _items;
It is much faster. But how to perform a search dynamically using each of the marked items in the "checklist" so that I can exit with a clean output?
My question is: how can I replace a lambda string with a variable searchkeywords?
This is the code that works, but it takes a lot of time:
listBox1.Items.Clear();
List<string> substrings = new List<string>();
foreach (var item in checkedListBox1.CheckedItems)
{
substrings.Add(item.ToString().Substring(0, 4));
}
, richtextbox .