c# - How to add unique words and their number of occurrences in a List -
i trying write program in c# read text file , count number of times each unique word shows , keeps track of words in file. example, in string "this text , it" get:
this - 2 - 2 - 1 text - 1 , - 1 - 1
is there easy way accomplish this? new c# , have not seen many things understand searching this.
edit:
so here code have tried. seems list returned has last word in file on , on , count off. though try rid of capitals , periods, still show up.
public override list<wordentry> getwordcount() { list<wordentry> words = new list<wordentry>(); wordentry wordentry = new wordentry(); string[] tokens = null; string line, temp; int count = 0, index = 0; while ((line = input.readline()) != null) { temp = regex.replace(line, @"\([0-9].\)", ""); temp.tolower(); tokens = temp.split(null); (int = 0; < tokens.length; i++) { wordentry.word = tokens[i]; foreach (var word in tokens) { if (word == tokens[i]) count++; }//end foreach wordentry.wordcount = count; words.add(wordentry); }//end }//end while return words; }//end getwordcount
the first step streamreader class...the streamreader class used read text file...then split each line of text file string[]...after have gotten far ..you can loop thru string[] foreach loop , count number of time word in array.