如何在Java中从String中消除重复的单词? [英] How can I eliminate duplicate words from String in Java?

查看:369
本文介绍了如何在Java中从String中消除重复的单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ArrayListString s,它包含诸如以下的记录:

I have an ArrayList of Strings and it contains records such as:

this is a first sentence
hello my name is Chris 
what's up man what's up man
today is tuesday

我需要清除此列表,以便输出中不包含重复的内容.在上述情况下,输出应为:

I need to clear this list, so that the output does not contain repeated content. In the case above, the output should be:

this is a first sentence
hello my name is Chris 
what's up man
today is tuesday

如您所见,

第三个字符串已被修改,现在仅包含一个语句what's up man,而不是其中的两个. 在我的列表中,有一种情况,有时String是正确的,有时又如上所示被加倍.

as you can see, the 3rd String has been modified and now contains only one statement what's up man instead of two of them. In my list there is a situation that sometimes the String is correct, and sometimes it is doubled as shown above.

我想摆脱它,所以我想遍历此列表:

I want to get rid of it, so I thought about iterating through this list:

for (String s: myList) {

但是我找不到消除重复项的方法,特别是因为每个字符串的长度都不确定,因此我可能会有记录:

but I cannot find a way of eliminating duplicates, especially since the length of each string is not determined, and by that I mean there might be record:

this is a very long sentence this is a very long sentence

有时是短的:

single word singe word

也许有一些本机的Java函数吗?

is there some native java function for that maybe?

推荐答案

我建议使用正则表达式.我可以使用以下模式删除重复项:\b([\w\s']+) \1\b

I would suggest using regular expressions. I was able to remove duplicates using this pattern: \b([\w\s']+) \1\b

public class Main {
    static String [] phrases = {
            "this is a first sentence",
            "hello my name is Chris",
            "what's up man what's up man",
            "today is tuesday",
            "this is a very long sentence this is a very long sentence",
            "single word single word",
            "hey hey"
    };
    public static void main(String[] args) throws Exception {
        String duplicatePattern = "\\b([\\w\\s']+) \\1\\b";
        Pattern p = Pattern.compile(duplicatePattern);
        for (String phrase : phrases) {
            Matcher m = p.matcher(phrase);
            if (m.matches()) {
                System.out.println(m.group(1));
            } else {
                System.out.println(phrase);
            }
        }
    }
}

结果:

this is a first sentence
hello my name is Chris
what's up man
today is tuesday
this is a very long sentence
single word
hey

这篇关于如何在Java中从String中消除重复的单词?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆