c# - Comparing strings of text files -
i have 3 text files: file1 file2 , file3 of contain emails. file1 supposed have emails in there, file2 has emails a-m, , file 3 have emails n-z (this not important figure give little context.)
i writing console application program in c# @ these 3 files, , if there email not 1 should be, write masterfile needs added what.
for example, lets have email john@example.com
. if found in file1 not in file2, output of masterfile needs "this email needs added file2: john@example.com"
. if reversed, , email found in file2 not in file1, output should "this email needs added file1: john@example.com"
as part of code, answer looking needs in sort of foreach loop , if statements, little lost in need put in. if please me in figuring out have use in statements appreciate it. if has question of please feel free ask!
//making list file1 list<string> listfullpack = new list<string>(); string line; streamreader sr = new streamreader("file1"); while ((line = sr.readline()) != null) { listfile1.add(line); } sr.close(); //making list file2 list<string> listden1 = new list<string>(); string line1; streamreader sr1 = new streamreader("file2"); while ((line1 = sr1.readline()) != null) { listfile2.add(line1); } sr1.close(); //making list file3 list<string> listden2 = new list<string>(); string line2; streamreader sr2 = new streamreader("file3"); while ((line2 = sr2.readline()) != null) { listfile3.add(line2); } sr2.close(); //this double check emails in foreach (string element in listfullpack) { system.console.writeline(element); debug.writeline(element); if (element == "jimbob@example.com") { debugger.break(); } } //this compare file1 list file2 list var firstnotsecond = listfile1.except(listfile2).tolist(); var secondnotfirst = listfile2.except(listfile1).tolist(); //this compare file2 list file3 list var firstnotthird = listfile1.except(listfile3).tolist(); var thirdnotfirst = listfile3.except(listfile1).tolist(); //this compare file2 list file3 list var secondnotthird = listfile2.except(listfile3).tolist(); var thirdnotsecond = listfile3.except(listfile2).tolist(); foreach (string element in listfile1) // lost { if (!) { } }
you try simple this:
//making list file1 hashset<string> listfile1 = new hashset<string>(); string line; streamreader sr = new streamreader("file1"); while ((line = sr.readline()) != null) { listfile1.add(line); } sr.close(); //making list file2 hashset<string> listfile2 = new hashset<string>(); string line1; streamreader sr1 = new streamreader("file2"); while ((line1 = sr1.readline()) != null) { listfile2.add(line1); } sr1.close(); //making list file3 hashset<string> listfile3 = new hashset<string>(); string line2; streamreader sr2 = new streamreader("file3"); while ((line2 = sr2.readline()) != null) { listfile3.add(line2); } sr2.close(); ienumerable<string> allemails = listfile1.union(listfile2).union(listfile3); // double check emails foreach (string element in allemails) { if (!listfile1.contains(element)) system.console.writeline("file 1 missing " + element); int firstcharascii = element.trim().tolower()[0]; if (firstcharascii < 110) { // less "n" if (!listfile2.contains(element)) system.console.writeline("file 2 missing " + element); if (listfile3.contains(element)) system.console.writeline("file 3 erroneously contains " + element); } else { // "n" or greater if (!listfile3.contains(element)) system.console.writeline("file 3 missing " + element); if (listfile2.contains(element)) system.console.writeline("file 2 erroneously contains " + element); } }
keep in mind, number of emails grows large, list<string>.contains()
method becomes inefficient way of determining presence or absence. better suited hashset<string>
class. also, if whatever reason reading in unicode strings, need more robust method checking first character's value.
Comments
Post a Comment