Simple answer:
token = token.replace("&", "&");
Despite the name compared to replaceAll, the substitute does replaceAll, it just does not use the regular expression, which seems to be here (both in terms of performance and in terms of good practice), does not use regular ones when they have special requirements for characters that you will not pay attention to).
The answer to Sean Bright is probably as good as it is worth considering in terms of performance, if you don't have any additional target requirement for benchmarking performance and performance, if you already know that this code is a performance hot spot if this is your question. coming from. This, of course, does not deserve descending slopes. Just use StringBuilder instead of StringBuffer if you don't need synchronization.
So there is a somewhat deeper potential problem here. Character escaping is a known issue that many libraries address. You might want to consider packing the data in the CDATA section into XML, or you may prefer to use the XML library (including the one that comes with the JDK) to actually generate the XML correctly (so that it handles the encoding).
Apache also has a screen library as part of Commons Lang.
Yishai Aug 05 '09 at 17:58 2009-08-05 17:58
source share