2012-10-03 49 views

回答

10

使用String.replaceAll() method"[{}]"正则表达式:

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

弦乐replaced" TestAddCardForMerchant "

0

这应做到:

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

您可以使用String#replaceAll方法从您的字符串替换{}所有ocurrences .. [][{}]用于表示字符类..这意味着{}

另外,你需要用反斜杠来转义",因为它在Java中有特殊的含义。所以,这里是你的正则表达式,以实现你所需要的: -

"{test}".replaceAll("[{}]", "\"") 
+0

@NSPostWhenIdle ..谢谢..做到了。 :) –

4

如果您需要用配对替换{}

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