Java Counting # of occurrences of a word in a string

前端 未结 8 1850
再見小時候
再見小時候 2020-12-03 19:20

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 e

8条回答
  •  余生分开走
    2020-12-03 20:18

    To get the number of occurrence of a specific word use the below code

         Pattern pattern = Pattern.compile("Thewordyouwant");
            Matcher matcher = pattern.matcher(string);
            int count = 0;
            while(matcher.find())
                count++;
    

提交回复
热议问题