Java String ReplaceAll method giving illegal repetition error?

后端 未结 3 917
一整个雨季
一整个雨季 2020-12-01 23:28

I have a string and when I try to run the replaceAll method, I am getting this strange error:

String str = \"something { } , op\";
str = str.r         


        
3条回答
  •  情歌与酒
    2020-12-02 00:11

    Escape it:

    str = str.replaceAll("\\{", "\n"); 
    

    This is needed since the first argument to replaceAll() is a regular expression, and { has a special meaning in Java regular expressions (it's a repetition operator, hence the error message).

提交回复
热议问题