2011-05-01 58 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不再包含 '#' 字符