2
對不起,對於長名稱,但我在這裏作爲初學者不知所措......可能我找不到現有的解決方案,因爲我沒有知道要搜索的術語。用包含原始子字符串的條件子字符串替換多個出現的子字符串
我想要做的是用一些包含原始子字符串的條件子字符串替換字符串中的所有子字符串。一個例子可能更加清楚:
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 answerOUT = "Hello, it is <sync:0> <sync:1> me. It is <sync:0> <sync:1> me myself and I"
我的代碼:
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);
}
}
我無法弄清楚如何不能替代已經替換字符串的子部分。