2013-03-19 3 views
0

⋍ 또는 𖫻과 같은 일부 야생 문자가 포함 된 HTML 파일을 얻었으므로이를 총알로 바꾸고 싶습니다. 그래서이 방법을 썼습니다 :String # 바꾸기가 내 메서드에서 작동하지 않습니다

public String replaceAmps(String initial) 
{ 
    // This list will contain all the &amps; to replace. 
    ArrayList<String> amps = new ArrayList<String>(); 
    String res = initial; 
    int index = initial.indexOf("&amp;"); 
    // Get all the indexes of &amp. 
    while (index >= 0) 
    { 
     StringBuilder stb = new StringBuilder("&amp;"); 
     // Create a String until the next ";", for example &amp;#1091;<- this one 
     for(int i = index+5 ; initial.charAt(i) != ';' ; i++) stb.append(initial.charAt(i)); 
     stb.append(";"); 
     // Add the amp if needed in the list. 
     if(!amps.contains(stb.toString())) amps.add(stb.toString()); 
     index = initial.indexOf("&amp;", index + 1); 
    } 
    // Replace the Strings from the list with a bullet. 
    for(String s : amps) res.replace(s, "•"); 
    return res; 
} 

정확하게 모든 앰프를 제 목록에 추가했지만 교체가 작동하지 않습니다. 왜? 당신의 도움을 주셔서 감사합니다.

답변

8

문자열을 변경할 수 없습니다. replace 메서드는 대체 결과로 String을 반환하고 res은 수정하지 않습니다. 시도해보십시오.

for(String s : amps) res = res.replace(s, "•"); 
+0

죄송합니다. 바로 그 점을 잊어 버렸습니다. 고마워. – Rob

+1

최종 제품으로 하나의 StringBuilder를 유지하는 것이 좋습니다. 당신이 대체 할 문자가 마음에 들지 않기 때문에, 루프 본문은 변경되지 않은 부분'substring (oldIndex, newIndex)'를 추가하고 글 머리표를 추가 한 다음 oldIndex를 세미콜론을 지나서 전진시킵니다. 끝 부분에 꼬리를 추가하는 것을 잊지 마십시오. –

+0

참된 고마워. – Rob

관련 문제