I have requirement in which I have to find the no. of times a particular word appears in a file. For eg.
String str = "Hi hello how are you. hell and heaven. hell, gjh, hello,sdnc ";
Now in this string I want to count no. of times the word "hell" appeared. The count should include "hell" , "hell," all these words but not "hello". So according to the given string I want the count to be 2.
I used following approaches
1st:
int match = StringUtils.countMatches(str, "hell");
StringUtils is of org.apache.commons.lang3 library
2nd:
int count = 0;
Pattern p = Pattern.compile("hell");
Matcher m = p.matcher(str);
while (m.find()) {
count++;
}
3rd
int count =0;
String[] s = str.split(" ");
for(String word: s)
if(word.equals("hell")
count++;
the 1st two approaches gave 4 as answer and the 3rd approach gave 1 as answer.
Please suggest anyway in which I can get 2 as answer and fullfill my requirement.
You should use word boundary matchers in regex:
Pattern.compile("\\bhell\\b");
You can use a regular expression with the "\\b" word boundaries as follows:
int matches = 0;
Matcher matcher = Pattern.compile("\\bhell\\b", Pattern.CASE_SENSITIVE).matcher(str);
while (matcher.find()) matches++;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With