如何在java中从一个句子中获取搜索词的总出现次数

如何在java中从一个句子中获取搜索词的总出现次数,java,Java,如何获取搜索关键字在句子中的总出现次数。 例如:-如果搜索的关键字是“Hello World”,则输出应为“Hello”、“World”和“Hello World”的出现总数之和 谢谢。您可以使用一些匹配器来使用正则表达式实现这一点 import java.util.regex.*; class Test { public static void main(String[] args) { String inputToTest = "The string that y

如何获取搜索关键字在句子中的总出现次数。 例如:-如果搜索的关键字是“Hello World”,则输出应为“Hello”、“World”和“Hello World”的出现总数之和


谢谢。

您可以使用一些
匹配器来使用正则表达式实现这一点

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);
    }
}
import java.util.regex.*;
课堂测试{
公共静态void main(字符串[]args){
String inputToTest=“要测试的字符串”;

Pattern=Pattern.compile(“我不太喜欢像“嘿,你能帮我做这个吗?”)这样的问题。通过一些研究,你可以很快找到一个解决方案。无论如何,你可以检查我的答案来帮助你开始。java中内置了一个实用程序,可以为你做这件事。请参阅入门