用Java中的替换单词替换单词的第n次出现 [英] Replacing nth occurrence of word with a replacement word in Java
问题描述
我正在做与Java语言有关的作业.我正在通过套接字从网站读取文本文件:
BufferedReader br = new BufferedReader(inr);
while((line2 = br.readLine()) != null)
{...}
仅当n为奇数时,才需要用替换单词替换目标单词的第n次出现. 例如:
第2行被读取为"ces11111111".目标词是11;替换词是CS219. 因此,结果行是cesCS21911CS21911.
我该如何实现?请帮我完成作业.
这是我的代码:
public static String replaceIfOdd(String stringToChange,
String searchingWord, String replacingWord) {
final String separator = "#######";
String splittingString = stringToChange.replaceAll(searchingWord,
separator + searchingWord);
String[] splitArray = splittingString.split(separator);
String result = "";
for (int i = 0; i < splitArray.length; i++) {
if (i % 2 == 1)
splitArray[i] = splitArray[i].replace(searchingWord,
replacingWord);
result += splitArray[i];
}
System.out.println(result);
return result;
}
调用方式:
replaceIfOdd("ces11111111", "11", "CS219");
其背后的逻辑如下:
我将所有出现的searchingWord
替换为separator + searchingWord
.
之后,我使用split
函数拆分了结果字符串.
然后,我遍历数组的所有元素,仅当searchingWord
出现在数组中的奇数位置时,才进行正确的替换,同时,我只完成正确的替换就重新创建了字符串.>
希望这就是您要搜索的内容!
PS:我使用了字符串separator
来分解stringToChange
,显然,如果在您的stringToChange
中有这样的字符串,则该方法将无法为您提供正确的结果...我会如果您担心它,请尝试考虑其他实现. iao!
I am doing homework related to Java language. I am reading a text file from a website via socket with:
BufferedReader br = new BufferedReader(inr);
while((line2 = br.readLine()) != null)
{...}
I need to replace the nth occurrence of the target word with a replacement word only if n is odd. For example:
line2 is readed as "ces11111111". target word is 11 ; replacement word is CS219. Thus , result line is cesCS21911CS21911.
How can I achieve this ? Please help me to complete my homework.
Here is my code:
public static String replaceIfOdd(String stringToChange,
String searchingWord, String replacingWord) {
final String separator = "#######";
String splittingString = stringToChange.replaceAll(searchingWord,
separator + searchingWord);
String[] splitArray = splittingString.split(separator);
String result = "";
for (int i = 0; i < splitArray.length; i++) {
if (i % 2 == 1)
splitArray[i] = splitArray[i].replace(searchingWord,
replacingWord);
result += splitArray[i];
}
System.out.println(result);
return result;
}
Way to call it:
replaceIfOdd("ces11111111", "11", "CS219");
The logic behind this is the following:
I replace all the occurrences of the searchingWord
with separator + searchingWord
.
After that I just split the resulting string using the split
function.
Then I loop through all the elements of the array and I make the right replacement only when the searchingWord
appears in an odd position into the array and, at the same time, I just recreate the string with the right replacements done.
Hope this is what you were searching for!
P.S.: I used a string separator
in order to break down the stringToChange
, obviously if into your stringToChange
there was such a string, the method wouldn't give you the right result ... I will try to think about a different implementation if you are worried about it. Ciao!
这篇关于用Java中的替换单词替换单词的第n次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!