0
votes

I am currently working on saving non duplicate rows. My code is too wide to put it there, so I will just show you piece of it.

Let's say I have a textbox1 with 100 rows. If I click a button every row from textbox2 is copied to textbox1. Now textbox1 contains 120 rows of text.

What I do next is delete duplicate and empty rows from textbox1. Lets say i deleted 15 rows.

Now, my textbox1 contains 105 rows of text. And now i put another text in textbox2 and all process starts again.

That means textbox2 combined with textbox1 included 15 duplicate and 5 non-duplicate rows. I would like to show non-duplicate rows in textbox3.

Input 1

textbox1:
1
2
3
4
5

textbox2:
1
2

6


70

Output 1

textbox1:
1
2
3
4
5
6
70

textbox3:
6
70

Input 2

textbox1:
1
2
3
4
5
6
70

textbox2:
1
555


1
1
5


999

0

Output 2

textbox1:
1
2
3
4
5
6
70
555
999
0

textbox3:
6
70
555
999
0

My code to delete duplicate and blank lines:

textBox1.Text += "\r\n";
textBox1.Text += textBox2.Text;

textBox1.Text = Regex.Replace(textBox1.Text, @"^\s*$(\n|\r|\r\n)", "", RegexOptions.Multiline);


textBox1.Text = string.Join(Environment.NewLine, textBox1.Lines.Distinct());

I am adding a newline to textbox1, because without that first line of textbox2 is being added at the end of last line of textbox2.

Im stuck with exporting nonduplicates rows and I decided to ask there.

3

3 Answers

3
votes

You may use LINQ's Union() to add lines avoiding duplication and Except() to find non-duplicate rows. The Where() may help you filtering out the empty lines:

var lines1 = textBox1.Lines.Where(s => !string.IsNullOrWhiteSpace(s));
var lines2 = textBox2.Lines.Where(s => !string.IsNullOrWhiteSpace(s));
textBox1.Lines = lines1.Union(lines2).ToArray();
textBox3.Lines = lines2.Except(lines1).ToArray();

// If you need to append the non-duplicate contents instead of replacing
// it in the textBox3, remove the previous operation and uncomment the
// following line:
//textBox3.Lines = textBox3.Lines.Concat(lines2.Except(lines1)).ToArray();
2
votes

The best way here is to convert the text of each TextBox in List< string >, so you can use Linq and Distinct() extension method to get only non duplicate rows.

var rows1 = this.textBox1.Text.Split(new string[] { "\r\n" }).ToList();
var rows2 = this.textBox2.Text.Split(new string[] { "\r\n" }).ToList();
rows1.AddRange(rows2);
rows1 = rows1.Distinct().ToList();
0
votes

You can use this oneliner:

var distinct =
    string.Join(Environment.NewLine, new []
    {
        textbox1.Text,
        textbox2.Text// You can add as many input as needed
    }).Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries).ToList().Distinct();