package org.kodejava.commons.lang;
import org.apache.commons.lang3.StringUtils;
public class WordCountDemo {
public static void main(String[] args) {
// We have the source text we'll do the search on.
String source = "From the download page, you can download the Java " +
"Tutorials for browsing offline. Or you can just download " +
"the example.";
// The word we want to count its occurrences
String word = "you";
// Using StringUtils.countMatches() method we can count the occurrences
// frequency of a word/letter in the giver source of string.
int wordFound = StringUtils.countMatches(source, word);
// Print how many we have found the word
System.out.println(wordFound + " occurrences of the word '" + word +
"' was found in the text.");
}
}
Here is the result of the code above.
2 occurrences of the word 'you' was found in the text.
Maven Dependencies
<!-- https://search.maven.org/remotecontent?filepath=org/apache/commons/commons-lang3/3.12.0/commons-lang3-3.12.0.jar -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
Latest posts by Wayan (see all)
- How do I find Java version? - March 21, 2022
- How do I convert CSV to JSON string using Jackson? - February 13, 2022
- How do I convert CSV file to or from JSON file? - February 13, 2022