我有一行如下:
abcde, def, efgh, mnop, mno, pqr, abc, abcde, abcde, mnop, efg
在此, abcde 和 mnop 不止一次出现。我想更改所有出现的 abcde 和 mnop 的名称,以便它们都表示不同。
如何在不改变序列顺序的情况下做到这一点?
请注意,出现不止一次的字数(在某些情况下是两倍以上)是未知的。因此,需要找出不止一次出现的单词。
该行是一个String,希望将最终结果作为String进行处理。
提前致谢。
答案 0 :(得分:2)
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
希望有所帮助。
答案 1 :(得分:2)
重要的步骤是:
需要考虑的事项:
HashSet
进行检查,如果文字重复,则会使您的计划快,即使是长文本。我的示例使用HashSet#contains
。输入是:
abcde ,def,efgh, mnop ,mno,pqr,abc, abcde , mnop ,efg
输出结果为:
abcde ,def,efgh, mnop ,mno,pqr,abc, abcde1 , mnop1 ,efg
我的示例代码:
import java.util.*;
import java.util.stream.Collectors;
public class DuplicateAvoider {
public static void main(String[] args) {
// define your input
String input = "abcde, def, efgh, mnop, mno, pqr, abc, abcde, mnop, efg";
// create a set of unique items (intermediate result)
Set<String> itemsUnique = new HashSet<>();
// split input by separator pattern
String[] items = input.split(", ");
String output =
// turn items into a Java8-stream
Arrays.stream(items)
// "foreach" item
.map(item -> {
// default items to their original text
String newItem = item;
// as long as that text is already in the list of unique items...
for (int i = 1; itemsUnique.contains(newItem); i++) {
// append a suffix-number
newItem = item + i;
}
// remember, that this item is now in use
itemsUnique.add(newItem);
return newItem;
})
// join unique items to one, separated text
.collect(Collectors.joining(", "));
// print to console
System.out.println(output);
}
}
如果您想更改开头的单词并保持最后一次出现不变,那么我建议您这样做:
输入是:
abcde ,def,efgh, mnop ,mno,pqr,abc, abcde , mnop ,efg
输出结果为:
abcde1 ,def,efgh, mnop1 ,mno,pqr,abc, abcde , mnop ,efg
示例代码:
import java.util.*;
import java.util.stream.Collectors;
public class DuplicateAvoider {
public static void main(String[] args) {
// define your input
String input = "abcde, def, efgh, mnop, mno, pqr, abc, abcde, mnop, efg";
// create a set of unique items (intermediate result)
Set<String> itemsUnique = new HashSet<>();
// split input by separator pattern
String[] items = input.split(", ");
// reverse order of items
List<String> itemsList = new ArrayList<>(Arrays.asList(items));
Collections.reverse(itemsList);
List<String> output =
// turn items into a Java8-stream
itemsList.stream()
// "foreach" item
.map(item -> {
// default items to their original text
String newItem = item;
// as long as that text is already in the list of unique items...
for (int i = 1; itemsUnique.contains(newItem); i++) {
// append a suffix-number
newItem = item + i;
}
// remember, that this item is now in use
itemsUnique.add(newItem);
return newItem;
})
.collect(Collectors.toList());
// re-reverse order of items
Collections.reverse(output);
// join unique items to one, separated text
String out = output.stream().collect(Collectors.joining(", "));
// print to console
System.out.println(out);
}
}
答案 2 :(得分:1)
您可以使用String.split
将句子拆分为出现在其中的单词数组,然后在单词数组中查找出现多次的单词。改变这些词并重写句子。
答案 3 :(得分:0)
拆分字符串,再次进行操作和构建字符串。
String myString = "abcde, def, efgh, mnop, mno, pqr, abc, abcde, mnop, efg";
String delim = ", ";
String myStrings[] = myString.split(delim);
for (int i = 0; i < myStrings.length; i++) {
String temp = myStrings[i];
for (int j = i + 1; j < myStrings.length; j++) {
if (myStrings[j].equals(temp))
myStrings[j] = temp + i;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < myStrings.length; i++) {
if (i == myStrings.length - 1)
sb.append(myStrings[i]);
else
sb.append(myStrings[i] + delim);
}
System.out.println(sb.toString());
输出:
abcde,def,efgh,mnop,mno,pqr,abc,abcde0,mnop3,efg