Java String ReplaceAll method giving illegal repetition error?

后端 未结 3 915
一整个雨季
一整个雨季 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-01 23:57

    If you really intend to replace single characters and not regexes (which is what you seem to want to do here), you should use .replace(), not .replaceAll(). In spite of its name, .replace() will replace ALL occurrences, not just the first one.

    And in case you wonder, String implements CharSequence, so .replace("{", "\n") will work.

提交回复
热议问题