Aug. 11, 2011, 9:25 a.m.
IT

Wonderful Java and escape sequences...

Imagine you have this piece of text:

I am stupid\

And what to change it to

I am stupid\\

so that it may be saved correctly to the database (assume you are not using prepared statements). A first attempt might be:

new String(Base64Coder.decode(pFieldData)).replaceAll("\\","\\\\");

because you are clever and know about escaping a single \ with another one \\ to produce a \. Sadly that will not work. The \\ expands to one \, and that is one part of the \\ you need for the regular expression. This is what you need to do - insane:

new String(Base64Coder.decode(pFieldData)).replaceAll("\\\\","\\\\\\\\");