[英]Unable to replace certain string items in java
I want this function to replace the '@'s and '#'s with the words from the string array and output a list.我希望这个 function 用字符串数组和 output 列表中的单词替换“@”和“#”。
import java.util.ArrayList;
import java.util.List;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String strSpecialties = "hello, test, test2, test3";
strSpecialties.trim();
String []lstSpecialties = strSpecialties.split(",");
String newString = "<AggregateColumn AggregateFunction="+"\"Sum\" " +"ID="+"\"siteTotal#\"" + " AggregateColumn="+"\"@\" />";
for(int i=0; i< lstSpecialties.length; i++){
newString = newString.replace("#", lstSpecialties[i]);
newString = newString.replace("@", lstSpecialties[i]);
System.out.println(newString);
}
}
}
ouput:输出:
<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" />
<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" />
<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" />
what i want我想要的是
<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" />
<AggregateColumn AggregateFunction="Sum" ID="siteTotalTest" AggregateColumn="Test" />
<AggregateColumn AggregateFunction="Sum" ID="siteTotalTest2" AggregateColumn="Test2" />
This will only work once, because after the first iteration you have replaced the @s and #s with the values.这只会工作一次,因为在第一次迭代之后,您已将 @s 和 #s 替换为值。 To get it to work, you need a local copy of the variable inside your for loop.为了让它工作,你需要在你的 for 循环内有一个变量的本地副本。
Your code should look like您的代码应如下所示
for(int i=0; i< lstSpecialties.length; i++){
String replaceStr = newString;
replaceStr = replaceStr .replace("#", lstSpecialties[i]);
replaceStr = replaceStr .replace("@", lstSpecialties[i]);
System.out.println(replaceStr );
}
try now:现在试试:
for(int i=0; i< lstSpecialties.length; i++){
String newString = "<AggregateColumn AggregateFunction="+"\"Sum\" " +"ID="+"\"siteTotal#\"" + " AggregateColumn="+"\"@\" />";
newString = newString.replace("#", lstSpecialties[i]);
newString = newString.replace("@", lstSpecialties[i]);
System.out.println(newString);
}
You need to reset newString
to what it originally was.您需要将newString
重置为原来的样子。
The following should work for you:以下内容应该适合您:
String originalString = "<AggregateColumn AggregateFunction="+"\"Sum\" " +"ID="+"\"siteTotal#\"" + " AggregateColumn="+"\"@\" />";
String newString = originalString;
for(int i=0; i< lstSpecialties.length; i++){
newString = newString.replace("#", lstSpecialties[i]);
newString = newString.replace("@", lstSpecialties[i]);
System.out.println(newString);
newString = originalString;
}
Move the initialization inside the loop:在循环内移动初始化:
import java.util.ArrayList;
import java.util.List;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
String strSpecialties = "hello, test, test2, test3";
strSpecialties.trim();
String []lstSpecialties = strSpecialties.split(",");
for(int i=0; i< lstSpecialties.length; i++){
String newString = "<AggregateColumn AggregateFunction="+"\"Sum\" " +"ID="+"\"siteTotal#\"" + " AggregateColumn="+"\"@\" />";
newString = newString.replace("#", lstSpecialties[i]);
newString = newString.replace("@", lstSpecialties[i]);
System.out.println(newString);
}
}
}
You need to start again with a fresh copy of 'newString' in each iteration.您需要在每次迭代中重新开始使用“newString”的新副本。
Currently you do目前你做
for(int i=0; i< lstSpecialties.length; i++){
newString = newString.replace("#", lstSpecialties[i]);
Here newString
no longer contains a '#' char这里newString
不再包含 '#' 字符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.