lundi 20 avril 2015

Count the no. of occurrence of the exact word in a file using java

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.

Aucun commentaire:

Enregistrer un commentaire