sriram
sriram

Reputation: 9032

Replacing { with "", in java?

I have a string like this:

{ TestAddCardForMerchant }

I wish to replace {,} with "". Is it possible to do so?

Upvotes: 0

Views: 114

Answers (4)

Rohit Jain
Rohit Jain

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

xdazz
xdazz

Reputation: 160883

If you need to replace {} only with pair.

String result = str.replaceAll("\\{([^}]+)\\}", "\"$1\"");

Upvotes: 4

skunkfrukt
skunkfrukt

Reputation: 1570

This should do it:

myString.replaceAll("[{}]", "\"");

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143906

Using the String.replaceAll() method and the "[{}]" regular expression:

String replaced = "{ TestAddCardForMerchant }".replaceAll("[{}]","\"");

The String replaced will be " TestAddCardForMerchant ".

Upvotes: 10

Related Questions