Using Java, I want to go through the lines of a text and replace all ampersand symbols (&
) with the XML entity reference &
.
I scan the lines of the text and then each word in the text with the Scanner class. Then I use the CharacterIterator
to iterate over each characters of the word. However, how can I replace the character? First, Strings are immutable objects. Second, I want to replace a character (&
) with several characters(amp&;
). How should I approach this?
CharacterIterator it = new StringCharacterIterator(token);
for(char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
if(ch == '&') {
}
}