Java Counting # of occurrences of a word in a string

Posted by Doug on Stack Overflow See other posts from Stack Overflow or by Doug
Published on 2010-04-14T05:25:56Z Indexed on 2010/04/14 5:33 UTC
Read the original article Hit count: 234

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about string