gpt4 book ai didi

java - 如何处理包含 2 个相同单词的行

转载 作者:行者123 更新时间:2023-11-30 06:13:42 25 4
gpt4 key购买 nike

我有一行如下:abcde、def、efgh、mnop、mno、pqr、abc、abcde、abcde、mnop、efg

在此,abcdemnop 出现了不止一次。我想更改所有出现的 abcdemnop 的名称,以便它们都以不同的方式表示。

如何在不改变序列顺序的情况下做到这一点?

请注意,出现不止一次(在某些情况下甚至超过两次)的单词是未知的。所以需要弄清楚单词出现不止一次是要做的。

该行是一个字符串,希望最终结果作为一个字符串进行处理。

提前致谢。

最佳答案

public class StringMod {
public static void main(String[] args) {
String text = "abcde, def, efgh, mnop, mno, pqr, abc, abcde, mnop, efg, abcde";
String[] sp = text.split(", ");
int count = 1;
for(int i=0;i<sp.length;i++){
count = 1;
for(int j=i+1;j<sp.length;j++){
if(sp[i].equals(sp[j])){
count++;
sp[j]=sp[j]+" "+count;
}
}
}

String returnString = "";
for(int i=0;i<sp.length-1;i++)
returnString+=sp[i]+", ";
returnString+=sp[sp.length-1];
System.out.println(returnString);
}
}

像这样的东西应该适合你....

输入:

abcde, def, efgh, mnop, mno, pqr, abc, abcde, mnop, efg, abcde

输出:

abcde, def, efgh, mnop, mno, pqr, abc, abcde 2, mnop 2, efg, abcde 3

希望对您有所帮助。

关于java - 如何处理包含 2 个相同单词的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31565713/

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