String newBody="abcdef".replaceAll("d","h\\$h");
这样不行吗?

解决方案 »

  1.   

    String newBody="abcdef".replaceAll("d","h\$h");
    单"\"
      

  2.   

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html对不起上面说错了
    应该是双斜线转义 "\\"
    string的replaceAll用到了regex包里的功能
    regex里面 $ 代表 The end of a line 所以必须escape掉java.lang.String
    public String replaceAll(String regex,
                            String replacement)
    Replaces each substring of this string that matches the given regular expression with the given replacement.
    An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression
    java.util.regex.Pattern.compile(regex).matcher(str).replaceAll(repl)