Closed Yzkkk closed 2 years ago
Use two backslashes before the [ brackets. Like so:
String temp = a.replaceAll("\\[]\\[hi]", "hey");
I don't know why backslashes are not required for the ] brackets. I tried
String temp = a.replaceAll("\\[\\]\\[hi\\]", "hey");
first and it gave me the same result, but IntelliJ recommended removing the apparently redundant escapes before the ] brackets so I did.
The string is treated as regex so it probably has something to do with that.
thank you!
Since [ and ] are special characters in a string, if i write something like String temp = a.replaceAll("[ ][hi]", "hey"); it is not going to work.
I remember we needed to use some '/' and '\' but couldn't remember exactly how :(