0
⋍
또는 𖫻
과 같은 일부 야생 문자가 포함 된 HTML 파일을 얻었으므로이를 총알로 바꾸고 싶습니다. 그래서이 방법을 썼습니다 :String # 바꾸기가 내 메서드에서 작동하지 않습니다
public String replaceAmps(String initial)
{
// This list will contain all the &s; to replace.
ArrayList<String> amps = new ArrayList<String>();
String res = initial;
int index = initial.indexOf("&");
// Get all the indexes of &.
while (index >= 0)
{
StringBuilder stb = new StringBuilder("&");
// Create a String until the next ";", for example &#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("&", index + 1);
}
// Replace the Strings from the list with a bullet.
for(String s : amps) res.replace(s, "•");
return res;
}
정확하게 모든 앰프를 제 목록에 추가했지만 교체가 작동하지 않습니다. 왜? 당신의 도움을 주셔서 감사합니다.
죄송합니다. 바로 그 점을 잊어 버렸습니다. 고마워. – Rob
최종 제품으로 하나의 StringBuilder를 유지하는 것이 좋습니다. 당신이 대체 할 문자가 마음에 들지 않기 때문에, 루프 본문은 변경되지 않은 부분'substring (oldIndex, newIndex)'를 추가하고 글 머리표를 추가 한 다음 oldIndex를 세미콜론을 지나서 전진시킵니다. 끝 부분에 꼬리를 추가하는 것을 잊지 마십시오. –
참된 고마워. – Rob