How to obtain the total occurrence of searched keyword in a sentence. Eg:-If searched keyword is "Hello World",the output should has the sum of total number of occurrences of "Hello","World" and "Hello World".
Thanks.
How to obtain the total occurrence of searched keyword in a sentence. Eg:-If searched keyword is "Hello World",the output should has the sum of total number of occurrences of "Hello","World" and "Hello World".
Thanks.
You could use some Matcher
to do that with regular expression.
import java.util.regex.*;
class Test {
public static void main(String[] args) {
String inputToTest = "The string that you want to test";
Pattern pattern = Pattern.compile("<YOU'RE REGEX PATTERN>");
Matcher matcher = pattern.matcher(inputToTest);
int count = 0;
while (matcher.find())
count++;
System.out.println(count);
}
}
You can do what you want with this. Just try it yourself now.