我想从字符串中替换一个字,就像string.replace("word", "different word");
工作的方式一样,只是有限的次数,可以说字apple
在字符串中被提及了10次,我想要中的5个变成oranges
,我怎么能做到这一点?用有限次数的字符串随机替换某个单词| Java
此外,如果可能,我希望它是随机的,所以它不会从第一个到第五个apple
但跳过,例如更改1st, 3rd, 4th, 7th, and 8th apple
会改变。
谢谢!
我想从字符串中替换一个字,就像string.replace("word", "different word");
工作的方式一样,只是有限的次数,可以说字apple
在字符串中被提及了10次,我想要中的5个变成oranges
,我怎么能做到这一点?用有限次数的字符串随机替换某个单词| Java
此外,如果可能,我希望它是随机的,所以它不会从第一个到第五个apple
但跳过,例如更改1st, 3rd, 4th, 7th, and 8th apple
会改变。
谢谢!
我会做随机子和那些使用replaceFirst
方法。对于子字符串,请使用substring(int beginIndex),其随机开始索引在0到lengthOfOriginalWord-1-lengthOfWordToReplace间隔之间。
try {
String originalWord = "alabalaportocalaORANGEalabalaportocalaORANGEalabalaportocalaORANGEalabalaportocalaORANGEalabalaORANGEORANGEaaaaaaa";
String replacedWord = "ORANGE";
String replacingWord = "APPLE";
String substringedWord = "";
// make sure this is smaller or equal with the number of occurrences, otherwise it's endless loop times
int numberOfReplaces = 3;
Random ran = new Random();
int beginIndex;
while (numberOfReplaces > 0) {
// random index between 0 and originalWord.length()-1-replacedWord.length()
beginIndex = ran.nextInt(originalWord.length()-1-replacedWord.length());
substringedWord = originalWord.substring(beginIndex);
if (substringedWord.contains(replacedWord)) {
originalWord = originalWord.substring(0, beginIndex) + substringedWord.replaceFirst(replacedWord, replacingWord);
numberOfReplaces--;
}
}
System.out.println(originalWord);
} catch (Exception exc) {
System.err.println("An error occurred: \n" + exc);
}
下面是输出运行五次后:
alabalaportocalaORANGEalabalaportocalaORANGEalabalaportocalaORANGEalabalaportocalaAPPLEalabalaAPPLEAPPLEaaaaaaa alabalaportocalaAPPLEalabalaportocalaAPPLEalabalaportocalaAPPLEalabalaportocalaORANGEalabalaORANGEORANGEaaaaaaa alabalaportocalaAPPLEalabalaportocalaORANGEalabalaportocalaORANGEalabalaportocalaAPPLEalabalaAPPLEORANGEaaaaaaa alabalaportocalaORANGEalabalaportocalaORANGEalabalaportocalaORANGEalabalaportocalaAPPLEalabalaAPPLEAPPLEaaaaaaa alabalaportocalaAPPLEalabalaportocalaORANGEalabalaportocalaAPPLEalabalaportocalaAPPLEalabalaORANGEORANGEaaaaaaa
为最详细和最好的例子选择了答案!谢谢! – Baruch 2013-04-27 21:04:43
您可以反复使用String#indexOf(String str, int fromIndex)
找到所有你要替换的单词的起始索引,然后随机选择其中的几个指标(例如,使用改组ArrayList<Integer>
,或者ArrayList<Integer>#remove(Random#nextInt(ArrayList#size))
),并使用级联构建新的字符串调用String#substring(int beginIndex, int endIndex)
ArrayList<Integer> indices = new ArrayList<>();
int fromIndex = str.indexOf(searchString);
while(fromIndex != -1) {
indices.add(fromIndex);
fromIndex = str.indexOf(searchString, fromIndex + 1);
}
for'String.indexof(String str,int fromIndex)',我为fromIndex放置了什么?你能解释一下这些功能吗?或者提供一个学习它的好资源? – Baruch 2013-04-27 01:51:15
看我的编辑。 'indexOf(String)'返回搜索字符串的第一个索引;然后在while循环中使用'indexOf(String,fromIndex)'来查找剩余的索引; 'indexOf(String,fromIndex)'搜索从fromIndex开始的搜索字符串的下一次出现,例如,如果'str =“abba”',那么'str.indexOf(“a”)'将返回'0',然后'str.indexOf(“a”,1)'将返回'3' - 它开始在索引1处搜索,即第一个'b'。有关更多详细信息,请参阅[String javadoc](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html)。 – 2013-04-27 01:59:20
好的,所以在我完成了所有的工作之后,使用'indices.remove(r.nextInt(indices.size()));'其中'r'是'Random'和'indices'是' ArrayList
概念,你可以做一个循环。您可以为文本中每次发生的苹果创建一个随机数。以最高价值5取代它们。究竟是什么问题?你找不到苹果词或者你不能替换它们吗? – 2013-04-27 01:38:34
关键是要节省时间,当然被替换的字符串不会只包含“apple”,字符串大约500字长,并且有很多字符串,不仅仅是一个,我试图找到一个短的因为我知道我将在未来更多地使用它来达到我的目的,所以要做得更快。 – Baruch 2013-04-27 01:48:05