Java Counting # of occurrences of a word in a string
- by Doug
I have a large text file I am reading from and I need to find out how many times some words come up. For example, the word "the". I'm doing this line by line each line is a string.
I need to make sure that I only count legit "the"'s the the in other would not count. This means I know I need to use regular expressions in some way. What I was trying so far is this:
numSpace += line.split("[^a-z]the[^a-z]").length;
I realize the regular expression may not be correct at the moment but I tried without that and just tried to find occurrences of the word the and I get wrong numbers to. I was under the impression this would split the string up into an array and how many times that array was split up was how many times the word is in the string. Any ideas I would be grateful.