Java.replaceAll implicitly uses Regex for replacement. This means that it is $foointerpreted as a regular expression pattern, and $is special in the regular expression (which means "end of line").
You need to avoid $how
String s3 = s2.replaceAll("\\$foo", "damn");
, Pattern.quote Java β₯1.5, , Matcher.quoteReplacement.
String s3 = s2.replaceAll(Pattern.quote("$foo"), Matcher.quoteReplacement("damn"));
Java β₯1.5 .replace .
String s3 = s2.replace("$foo", "damn");
: http://www.ideone.com/Jm2c4