簡體   English   中英

無法替換 java 中的某些字符串項

[英]Unable to replace certain string items in java

我希望這個 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);
        }
    }
}

輸出:

<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" />

這只會工作一次,因為在第一次迭代之后,您已將 @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 );
}

現在試試:

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

您需要將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;
}

在循環內移動初始化:

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

您需要在每次迭代中重新開始使用“newString”的新副本。

目前你做

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

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

這里newString不再包含 '#' 字符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM