shital sonavane wrote:I found it. The answer is str.replaceAll("\\?","\\\\?").
Yea, there are three things going on.
1. The backslash has meaning to a string literal and needs to be escaped.
2. The backslash and the question mark has meaning to a regular expression and both needs to be escaped.
3. The backslash has meaning to a regex replacement string and needs to be escaped.
You need to understand how all three (and how it interacts) to get this one right.
Other option (with Java 5), is to use the replace method (that takes two strings) instead. This way, you only need to understand the first, as this version doesn't use regular expressions.