How to check if a string is unique in a file and if it is write to another file in C#? -
i have 2 files f1.txt , f2.txt. f1.txt contains contents like
u1,u2 u1,u5 u3,u4 u2,u1 u3,u4
essentially u1,u2 , u2,u1 mean same . want write distinct contents file f2.txt i.e. after writing f2.txt should contain
u1,u2 u1,u5 u3,u4
i tried below did not work out.
using system; using system.collections.generic; using system.io; using system.linq; using system.text; using system.threading.tasks; namespace stringinfiletechchef { class program { static void main(string[] args) { string line = ""; using (streamreader sr = new streamreader(@"c:\users\chiranjib\desktop\f1.txt")) { while((line=sr.readline()) !=null) { if (!file.readalltext(@"c:\users\chiranjib\desktop\f2.txt").contains(line)) { //char[] array = line.tochararray(); //array.reverse(array); //string temp = new string(array); string temp1 = line.substring(0, 2); string temp2 = line.substring(3, 2); if (!file.readalltext(@"c:\users\chiranjib\desktop\f2.txt").contains(temp2 + "," + temp1)) { using (streamwriter sw = new streamwriter(@"c:\users\chiranjib\desktop\f2.txt")) { sw.writeline(line); console.writeline(line); } } } } } console.readkey(); } } }
what missing ? how achieve scenario.
here how it:
take each line , split string[]
sort string[]
join string[]
string
take distinct strings
var distinct = file.readlines("textfile2.txt") .select(l => string.join(",", l.split(',').orderby(i => i))) .distinct(); file.writealllines("f2.txt", distinct);
Comments
Post a Comment