Re: String.replace() in practice
On May 22, 4:25 am, Aljosa Mohorovic <aljosa.mohoro...@gmail.com>
wrote:
i currently have this:
String tmp = "this is text for flash player";
tmp = tmp.replace("š", "=C5=A1");
tmp = tmp.replace("Š", "=C5 ");
tmp = tmp.replace("<br />", "<br>");
...
and this works but i was wondering if there is a better way to do
this?
Aljosa
There isn't a simpler way to do this, if thats what your question
was. There are more efficient methods, but most of them are quite a
bit more complex.
Oh, also, you should consider the fact that String.replace only
replaces the FIRST instance in the string. you can use replaceAll,
but replaceAll uses regex (so you have to make sure that your search/
replace text is escaped appropriately). Also, being regex, it does
actually slow things down somewhat.
I say, stick with the easiest code unless it has been proven to be too
inefficient. If you find your program is taking up to many resources,
you could then consider using some sort of finite state machine to
parse and replace everything in one go. Actually, I wish Sun would
add to the JDK a method String.translate(java.util.Map<String, String>
translations); The intent would be that any key in the String would
be replaced by the corresponding value. This could be somewhat
optimized.