我有一个这样的字符串:
{ TestAddCardForMerchant }
我希望将 ,{替换}为"". 有可能这样做吗?
使用String.replaceAll()方法和"[{}]"正则表达式:
String replaced = "{ TestAddCardForMerchant }".replaceAll("[{}]","\"");
字符串replaced将是" TestAddCardForMerchant ".
如果您只需要更换{}成对。
String result = str.replaceAll("\\{([^}]+)\\}", "\"$1\"");
这应该这样做:
myString.replaceAll("[{}]", "\"");
您可以使用String#replaceAll方法替换{}字符串中所有出现的 ..
[]in[{}]用于表示字符类.. 这意味着{或}..
此外,您需要"使用反斜杠转义,因为它在 Java 中具有特殊含义。所以,这是您的正则表达式,以实现您的需要:-
"{test}".replaceAll("[{}]", "\"")