2011-05-01 4 views
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(","); 

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

나는 첫 번째 반복 후에는 @s 및 #S를 교체했기 때문에 한 번만 작동합니다

<AggregateColumn AggregateFunction="Sum" ID="siteTotalHello" AggregateColumn="Hello" /> 
<AggregateColumn AggregateFunction="Sum" ID="siteTotalTest" AggregateColumn="Test" /> 
<AggregateColumn AggregateFunction="Sum" ID="siteTotalTest2" AggregateColumn="Test2" /> 

답변

1

지금 시도처럼

코드는 보일 것입니다.

은 당신을 위해 다음과 같은 작업을해야합니다 :

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

이 원하는 것을 값으로 작동 시키려면 for 루프 내부에 변수의 로컬 사본이 필요합니다.

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++){ 
    String replaceStr = newString; 

    replaceStr = replaceStr .replace("#", lstSpecialties[i]); 
    replaceStr = replaceStr .replace("@", lstSpecialties[i]); 
    System.out.println(replaceStr); 
} 
1

루프 내부의 초기화를 이동 : 각 반복에서 'newstring 인수로 쓰여진'의 새로운 복사본을 다시 시작해야

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

.

현재 당신은

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

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

다음은하지 않는다 newString는 더 이상 '#'문자를 포함

관련 문제