O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


JAVA HACK

Avoid the dreaded replaceAll method
The replaceAll method of the java.lang.String class has an unexpected behavior. Escaped double quotes (\") are unescaped. A sound replacement is org.apache.commons.lang.StringUtils#replace(String, String, String).

Contributed by:
Unknown User anonymous2
[06/29/04 | Discuss (4) | Link to this hack]

The java.lang.String#replaceAll(String, String) method has a unexpected behavior. Escaped double quotes are unescaped. So: "a string".replaceAll("string", "\"TEST\"") will not produce a \"TEST\". Instead, the escaped double quotes will be unescaped and thus the output will be a "TEST". A sound replacement for the replaceAll method is org.apache.commons.lang.StringUtils#replace(String, String, String). This method will not unescape the double quotes. Hack submitted by Steven Devijver.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.