在字符串中查找子字符串时遇到问题

时间:2014-10-25 17:18:03

标签: java string

我需要打印所有在字符串中出现一次的单词。

例如

input:s = "Java Ruby PHP.  Java is good. PHP please looks at Java"

输出:

Java
PHP

1 个答案:

答案 0 :(得分:0)

    Set<String> words = new HashSet<>();
    String str = "Java Ruby PHP.  Java is good. PHP please looks at Java";
    Matcher mat = Pattern.compile("\\b(\\w+)(?=\\b.*\\1)").matcher(str);
    while(mat.find()){
        words.add(mat.group(1));
    }
    System.out.println(words);

此外,您可以按字分割字符串并使用Map计算计数。然后过滤带有count&gt;的单词1。

相关问题