Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将多次出现的子字符串替换为包含原始子字符串的条件子字符串_Java_String_Replace_Substring - Fatal编程技术网

Java 将多次出现的子字符串替换为包含原始子字符串的条件子字符串

Java 将多次出现的子字符串替换为包含原始子字符串的条件子字符串,java,string,replace,substring,Java,String,Replace,Substring,很抱歉标题太长,但作为初学者,我在这里不知所措。。。可能我找不到现有的解决方案,因为我不知道要搜索的术语 我要做的是用包含原始子字符串的条件子字符串替换字符串中的所有子字符串。一个例子可能更清楚: String answerIN = "Hello, it is me. It is me myself and I." //should become something like: String answerOUT = "Hello, it is <sync:0> me. It is

很抱歉标题太长,但作为初学者,我在这里不知所措。。。可能我找不到现有的解决方案,因为我不知道要搜索的术语

我要做的是用包含原始子字符串的条件子字符串替换字符串中的所有子字符串。一个例子可能更清楚:

String answerIN = "Hello, it is me. It is me myself and I."
//should become something like: 
String answerOUT = "Hello, it is <sync:0> me. It is <sync:1> me myself and I"
String answerIN=“你好,是我。是我自己和我。”
//应该变成这样:
String answerOUT=“你好,是我。是我自己和我”
所以子字符串“me”应该由它自身加上一些条件的东西来替换。到目前为止,我尝试的方法不起作用,因为我一直在替换替换的子字符串。因此,我最终得出以下结论:

String answerOUT = "Hello, it is <sync:0> <sync:1> me. It is <sync:0> <sync:1> me myself and I"
String answerOUT=“你好,是我。是我自己和我”
我的代码:

        String answerIN = "Hello, it is me. It is me myself and I.";
        String keyword = "me";
        int nrKeywords = 2; //I count this in the program but that's not the issue here

        if (nrKeywords != 0){
            for (int i = 0; i < nrKeywords; i++){
                action = " <sync" + i + "> " + keyword;
                answerIN = answerIN.replace(keyword, action);
                System.out.println("New answer: " + answerIN);
            }
        }
String answerIN=“你好,是我。是我自己和我。”;
字符串关键字=“我”;
int=2//我把这算在节目里,但这不是问题所在
如果(nR关键字!=0){
对于(int i=0;i
我不知道如何不替换已经替换的字符串的子字符串部分。

string#replace
将始终用您想要替换的内容替换您正在查找的
字符串的每一次出现。因此,对于常规的
字符串#replace
,这是不可能的,因为没有“仅从这里到那里进行替换”

您可以使用
String
的子字符串方法来替换每次出现的情况:

String input = "Hello, it is me. It is me myself and I.";
String output = "";
String keyword = "me";
int nextIndex = input.indexOf(keyword), oldIndex = 0, counter = 0;

while(nextIndex != -1) {
    output += input.substring(oldIndex, nextIndex-1) + " <sync:" + counter + "> ";
    oldIndex = nextIndex;
    nextIndex = input.indexOf(keyword, nextIndex+1);
    ++counter;
}
output += input.substring(oldIndex);
System.out.println(output);
String input=“你好,是我,是我自己和我。”;
字符串输出=”;
字符串关键字=“我”;
int-nextIndex=input.indexOf(关键字),oldIndex=0,counter=0;
while(nextIndex!=-1){
输出+=输入.子字符串(oldIndex,nextIndex-1)+”;
oldIndex=nextIndex;
nextIndex=input.indexOf(关键字,nextIndex+1);
++计数器;
}
输出+=输入子字符串(oldIndex);
系统输出打印项次(输出);
O/p

你好,是我。是我自己和我。
Hello, it is <sync:0> me. It is <sync:1> me myself and I.