简体   繁体   中英

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.

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. To get it to work, you need a local copy of the variable inside your for loop.

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.

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.

Currently you do

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

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

Here newString no longer contains a '#' char

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM