2012-10-03 56 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\"");