If i got a text file
“dont run if you cant hide, or you will be broken in two strings, your a evil man”
and i want to count how many times the word you is in the text file, and put that value in to a int variable.
how do i go about doing somthing like that?
Assuming there are regular line breaks then if the file is huge this would be less memory intensive than some other approaches here. Uses Jason’s counting method:
Or, combining the Scoregraphic’s answer here with a IEnumerable method:
You could get a nifty one-liner
[Edited because System.IO.File now supports enumerating the lines of a file, removing need for hand rolled method of doing the same thing described above]
Or using framework method
File.ReadLines()you could reduce this to: