Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Replace Curly braces in java?

Tags:

java

I am getting error when try to replace regular expression in java.

for example:

String h = "{hiren:}";

h=h.replaceAll(":}", ":\"\"}");

Please give me solution. Thanks

like image 806
Hiren Avatar asked Mar 25 '15 13:03

Hiren


1 Answers

You need to double-escape some special characters in Patterns.

String#replaceAll takes regular expressions, hence:

String h = "{hiren:}"; h=h.replaceAll(":\\}", ":\"\"}");

Otherwise, you can use String#replace with no regular expression nor escaping needed.

String h = "{hiren:}"; h=h.replace(":}", ":\"\"}");

It's a commonly mistaken assumption to believe String#replace will not replace all occurrences.

like image 56
Mena Avatar answered Sep 28 '22 10:09

Mena