Not able to replace all for dollar sign

前端 未结 3 685
耶瑟儿~
耶瑟儿~ 2020-12-03 21:21

can anyone advise why i encountered index out of bouns exception when running this method to replace the value by $ sign?

E.g. i pass in a message

3条回答
  •  星月不相逢
    2020-12-03 22:12

    It is special character you need to use escape character

    Try with this \\$

    and it doesn't make sense in your code you are trying to replacing the content with same

    String message = "$$hello world $$";
    message = message.replaceAll("\\$", "_");
    System.out.println(message);
    

    output

    __hello world __
    

    Update

       String message = "$hello world $$";
       message = message.replaceAll("$", "\\$");
       System.out.println(message);
    

    output

     $hello world $$
    

提交回复
热议问题