无法替换java中的某些字符串项

时间:2011-05-01 15:03:20

标签: java

我希望这个函数用字符串数组中的单词替换'@'和'#'并输出一个列表。

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);
        }
    }
}

输出中:

<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" />
<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" />
<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" />

我想要什么

<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" />
<AggregateColumn AggregateFunction="Sum" ID="siteTotalTest" AggregateColumn="Test" />
<AggregateColumn AggregateFunction="Sum" ID="siteTotalTest2" AggregateColumn="Test2" />

5 个答案:

答案 0 :(得分:3)

这只会工作一次,因为在第一次迭代后你已经用值替换了@s和#s。要使其工作,您需要在for循环中使用变量的本地副本。

您的代码应该是

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 );
}

答案 1 :(得分:1)

立即尝试:

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);
}

答案 2 :(得分:1)

您需要将newString重置为原来的状态。

以下内容对您有用:

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;
}

答案 3 :(得分:1)

在循环内移动初始化:

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);
        }
    }
}

答案 4 :(得分:1)

你需要在每次迭代中重新开始使用'newString'的新副本。

目前你做

for(int i=0; i< lstSpecialties.length; i++){

        newString = newString.replace("#", lstSpecialties[i]);

此处newString不再包含'#'字符