rober71
rober71

Reputation: 13

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

Upvotes: 1

Views: 721

Answers (5)

Heiko Rupp
Heiko Rupp

Reputation: 30934

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

Upvotes: 1

Mihai Toader
Mihai Toader

Reputation: 12243

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

Upvotes: 1

AbdullahC
AbdullahC

Reputation: 6730

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

Upvotes: 1

GuruKulki
GuruKulki

Reputation: 26418

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

Upvotes: 1

Codemwnci
Codemwnci

Reputation: 54884

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

Upvotes: 3

Related Questions