简体   繁体   中英

Replacing { with “”, in java?

I have a string like this:

{ TestAddCardForMerchant }

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

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

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

The String replaced will be " TestAddCardForMerchant " .

如果您只需要将{}替换为pair。

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

应该这样做:

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

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("[{}]", "\"")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM