2011-05-01 39 views
1

我希望此函數用字符串數組中的單詞替換'@'和'#'並輸出一個列表。無法取代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" /> 

回答

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

這是因爲第一次迭代之後,你已經取代了@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

您需要重置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; 
} 
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); 
     } 
    } 
} 
1

你需要用「newString」在每個迭代一個新的副本重新開始。

目前你做

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

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

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