RSS RSS feed | Atom Atom feed | |

Double escape clause

When confusing things happen to smart people

I must have had 10 questions on escape characters in Java in the last six weeks. Most of them were experienced folks that were project hopping and ran into an unexpected problem. In the end, they just missed some basics in the way languages handle escape characters. Here are a couple traps that caught the distracted.

Everyone knows that special charachters need to be escaped in Strings.

If you mess up the escape character the results are obvious.

Except that some times you can forget and the results are not so obvious.

String manipulation has its own pit falls now that regular expression has been added to the API.

Even if you remember to escape characters in your string you need to remember them in you replace method.

Turns out that replace uses Regular Expression so you need to escape the string once for Java and once for regex.

And don't forget, it takes three escape characters to escape twice.

And don't forget, it takes three escape characters to escape twice.

Remember that there is a similar need when JavaScript is being created from a JSP. A single escape will not creat valid JavaScript.

Double escapes generates the needed string after the Java compiler and the JavaScript processor evaluates it.

Tags :



Add a comment Send a TrackBack