0

您将如何检查包含多行的多个 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);
                    }
                }
            }
        }
    }
}
4

2 回答 2

0

使用 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)
    }
}
于 2013-07-09T21:08:56.827 回答
0

这就是我为得到我所追求的结果所做的。如上所述循环遍历 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();
于 2013-07-10T22:46:43.713 回答