如何检查包含多行的多个RichTextbox中的唯一行和/或重复行
本文关键字:唯一 RichTextbox 何检查 检查 包含多 | 更新日期: 2023-09-27 18:21:39
如何检查包含多行的多个RichTextbox中的唯一行和/或重复行。我已经想好了如何在richTextboxes中循环并获取数据,但我正在努力研究逻辑,看看它是否是唯一的。如果代码像泥一样清晰,我深表歉意。
List<string> DistinctItems = new List<string>();
List<string> DupilcatedItems = new List<string>();
List<string> FirstItemsList = new List<string>();
List<string> CompareItemsList = new List<string>();
int ElementIndex = 0;
foreach (RichTextBox c in tableLayoutPanel1.Controls)
{
if (c.Text != null)
{
FirstItemsList.Add(c.Text.Replace("'n", Environment.NewLine).ToString());
if (CompareItemsList.Count == 0)
{
//Have to add the first batch
foreach (string str in FirstItemsList)
{
txtMixerTextBox.AppendText(str);
txtDistinctItems.AppendText(str);
DistinctItems.Add(str);
ElementIndex++;
}
CompareItemsList.Add(c.Text.Replace("'n", Environment.NewLine).ToString());
if (CompareItemsList.Count() > 0)
{
//OK we've gone through the first set
foreach (string s in CompareItemsList)
{
if (DistinctItems.Contains(s))
{
//It's a duplicate see if it's in the duplicate list
if (DupilcatedItems.Contains(s))
{
//OK it's in the list we don't have to add it
//See if it's in the textbox
if (!txtDuplicateItems.Text.Contains(s))
{
//OK it's not in the textbox let's add it
txtDuplicateItems.AppendText(s);
}
}
}
else
{
//It's not there so add it
DupilcatedItems.Add(s);
//now see if it's in the Distinct Textbox
if (!txtDistinctItems.Text.Contains(s))
{
//add it
txtDistinctItems.AppendText(s);
}
txtMixerTextBox.AppendText(s);
}
}
}
}
}
}
使用String.Split。例如:
foreach (RichTextBox c in tableLayoutPanel1.Controls)
{
if (!c.Text.IsNullOrWhiteSpace)
{
string[] lines = c.Text.Split(''n');
string[] uniqueLines = GetUniqueLines(lines);//Some line-uniqueness checking algorithm
c.Text = String.Join(''n',uniqueLines)
}
}
这就是我所做的,以获得我想要的结果。如上所述,我在RichTextboxes中循环,将列表写入一个文件,去掉空行(它们来自哪里,我一点也不清楚),将文件读取到一个新列表中,然后从中获得不同的列表。我认为空行可能把我搞砸了,也可能是因为我(再次)循环列表中的字符串,从而给自己带来了重复。我可能会因此受到打击,但它对我有效。
List<string> SortingList = new List<string>();
using (StreamReader r = new StreamReader("DistinctItemsNoBlankLines.txt"))
{
string line;
while ((line = r.ReadLine()) != null)
{
SortingList.Add(line);
}
}
List<string>DistinctSortingList = SortingList.Distinct().ToList();
foreach (string str in DistinctSortingList)
{
int index = 0;
while ( index < DistinctSortingList.Count() -1)
{
if (DistinctSortingList[index] == DistinctSortingList[index + 1])
DistinctSortingList.RemoveAt(index);
else
index++;
}
}
txtDistinctItems.Lines = DistinctSortingList.ToArray();