murderdoll Posted June 16, 2009 Share Posted June 16, 2009 Hello, I want to count how many times a specific word has been repeated in a text document, what would be the best way to do that? Thanks Link to comment https://www.neowin.net/forum/topic/785228-vbnet-count-a-specific-word-in-a-text-document/ Share on other sites More sharing options...
0 wrack Posted June 16, 2009 Share Posted June 16, 2009 (edited) Open the text document and then use Regular Expression matching and then get the count of the matching instances. Edited June 16, 2009 by wrack Link to comment https://www.neowin.net/forum/topic/785228-vbnet-count-a-specific-word-in-a-text-document/#findComment-591144542 Share on other sites More sharing options...
0 Andre S. Veteran Posted June 16, 2009 Veteran Share Posted June 16, 2009 (edited) EDIT: nvm my first example with a linq expression, it was way too inefficient. As wrack says, the best way is to use Regex. Imports System.IO Imports System.Text.RegularExpressions Module Module1 Sub Main() Dim text = File.ReadAllText("G:\USER\Documents\MyFile.log") Dim count = Regex.Matches(text, "a_word").Count Console.WriteLine(count) Console.ReadKey() End Sub End Module This code first stores the contents of MyFile.log as a string in "text". Then it counts the number of occurences of "a_word" in it. Edited June 16, 2009 by Dr_Asik Link to comment https://www.neowin.net/forum/topic/785228-vbnet-count-a-specific-word-in-a-text-document/#findComment-591145018 Share on other sites More sharing options...
0 Andre S. Veteran Posted June 16, 2009 Veteran Share Posted June 16, 2009 Actually, for even better efficiency, you should use a StreamReader instead of File.ReadAllText(). Using tr As TextReader = New StreamReader("G:\USER\Documents\MyFile.log") count = Regex.Matches(tr.ReadToEnd(), "a_word").Count End Using Link to comment https://www.neowin.net/forum/topic/785228-vbnet-count-a-specific-word-in-a-text-document/#findComment-591145152 Share on other sites More sharing options...
0 wrack Posted June 16, 2009 Share Posted June 16, 2009 Yeah Dr_Asik got it. Sorry I didn't post an example as I was typing that from my pda and I still am. I was just hoping that pointing you to a direction should help :) Link to comment https://www.neowin.net/forum/topic/785228-vbnet-count-a-specific-word-in-a-text-document/#findComment-591145492 Share on other sites More sharing options...
0 murderdoll Posted June 16, 2009 Author Share Posted June 16, 2009 Thanks a lot for the example Dr_Asik, cleared a lot for me. :) Link to comment https://www.neowin.net/forum/topic/785228-vbnet-count-a-specific-word-in-a-text-document/#findComment-591146700 Share on other sites More sharing options...
Question
murderdoll
Hello,
I want to count how many times a specific word has been repeated in a text document, what would be the best way to do that?
Thanks
Link to comment
https://www.neowin.net/forum/topic/785228-vbnet-count-a-specific-word-in-a-text-document/Share on other sites
5 answers to this question
Recommended Posts