First the reason I need to do this is because of the API in Apache's Commons StringUtils method:
StringUtils.replaceEach(String text, String[] searchList, String[] replacementList)
What I want to do is replace all the HTML special character encodings with the actual special character, which means that the searchList
and replacementList
arrays will be pretty large. How can I do this in an easy to read and maintain way?
Yes I could create two arrays but if I do this then it will be very easy to make mistakes. How do I know I'm not missing a special encoding, that I have the right position, etc. I would much rather have code where the encoding and the character are side by side to avoid any errors. I looked at a HashMap
but then you can only get the keys (encodings) and have to loop through to get the character values, which is not very performant, especially not if it's going to be run a lot. The same is true with a two dimensional array that you have to split each run.