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.
Showing messages 1 through 4 of 4.
-
Escape?
2004-11-08 04:03:34
eckes
[View]
-
RE: Avoid the dreaded replaceAll method
2005-03-07 10:31:48
dustbort
[View]
-
its really helpful about replaceAll method
2005-07-13 23:52:26
madhujava
[View]
-
RE: Avoid the dreaded replaceAll method
2006-08-07 09:25:07
npgall
[View]
|
Showing messages 1 through 4 of 4.
|
|
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.
|
|
a = "\"TEST\"";
for sure does not contain the escape char, because it is removed by the parser, this is not related to replaceAll. From javadoc:
An invocation of this method of the form str.replaceAll(regex, repl)
yields exactly the same result as the expression
Pattern.compile(regex).matcher(str).replaceAll(repl)
Greetings
Bernd