How to escape $ in java?

前端 未结 4 1432
别那么骄傲
别那么骄傲 2021-01-19 09:17

I am trying below code but getting error

String x = \"aaa XXX bbb\";
    String replace = \"XXX\";
    String y = \"xy$z\";
    String z=y.replaceAll(\"$\",          


        
4条回答
  •  离开以前
    2021-01-19 09:59

    Use replace() instead, which doesn't use regular expressions, since you don't need them at all:

    String x = "aaa XXX bbb";
    String replace = "XXX";
    String y = "xy$z";
    x = x.replace(replace, y);
    System.out.println(x);
    

    This will print aaa xy$z bbb, as expected.

提交回复
热议问题