Reputation: 9032
I have a string like this:
{ TestAddCardForMerchant }
I wish to replace {
,}
with ""
. Is it possible to do so?
Upvotes: 0
Views: 114
Reputation: 213311
You can use String#replaceAll method to replace all ocurrences of {}
from your string..
[]
in [{}]
is used to denote character class.. It means {
or }
..
Also, you need to escape "
with a backslash, as it has special meaning in Java..
So, here's is your regex, to achieve what you need :-
"{test}".replaceAll("[{}]", "\"")
Upvotes: 0
Reputation: 160883
If you need to replace {}
only with pair.
String result = str.replaceAll("\\{([^}]+)\\}", "\"$1\"");
Upvotes: 4
Reputation: 143906
Using the String.replaceAll()
method and the "[{}]"
regular expression:
String replaced = "{ TestAddCardForMerchant }".replaceAll("[{}]","\"");
The String replaced
will be " TestAddCardForMerchant "
.
Upvotes: 10