gpt4 book ai didi

java - 删除多次出现的单词

转载 作者:行者123 更新时间:2023-12-03 22:59:49 25 4
gpt4 key购买 nike

如何删除 String 中多次出现的单词?这里最难的是,我不知道它是哪个词。请参阅以下示例。

This is how how I tried to split a paragraph into a sentence sentence But, there is a problem My paragraph includes dates dates dates dates like Jan 13, 2014 , words includes like U S and numbers

这里,一些词有多次出现。 sentencedatesincludeshow 等词不止一次出现。请注意,这种重复可能不会彼此靠近,例如 includes。我想删除这些,所以它会像下面这样。

This is how I tried to split a paragraph into a sentence But, there is a problem My paragraph includes dates like Jan 13, 2014 , words like U S and numbers

请注意,删除多次出现并不意味着删除多次出现的单词的所有出现。它只会保留一个副本并删除其余副本。

就像上面一样,会有非常大的String,我不知道哪个单词出现了不止一次。我怎样才能做到这一点?

最佳答案

一次一个单词地复制文本,并在整个过程中忽略重复项。使用哈希集来跟踪重复项。

像这样的……

String text = "This is how how I tried to split a paragraph into a sentence sentence But, there is a problem My paragraph includes dates dates dates dates like Jan 13, 2014 , words includes like U S and numbers"; 
StringBuilder result = new StringBuilder();
HashSet<String> set = new HashSet<String>();
for(String s : text.split(" ")) {
if (!set.contains(s)) {
result.append(s);
result.append(" ");
set.add(s);
}
}
System.out.println(result);

您必须稍微修改一下才能正确处理标点符号,但这应该让您入门。

关于java - 删除多次出现的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21450932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com