[英]Replacing { with “”, in java?
我有一個像這樣的字符串:
{ TestAddCardForMerchant }
我希望將{
, }
替換為""
。 有可能這樣做嗎?
使用String.replaceAll()
方法和"[{}]"
正則表達式:
String replaced = "{ TestAddCardForMerchant }".replaceAll("[{}]","\"");
replaced
的字符串將為" TestAddCardForMerchant "
。
如果您只需要將{}
替換為pair。
String result = str.replaceAll("\\{([^}]+)\\}", "\"$1\"");
應該這樣做:
myString.replaceAll("[{}]", "\"");
您可以使用字符串#的replaceAll方法替換所有ocurrences {}
從您的字符串.. []
中的[{}]
用來表示字符類..這意味着{
或}
..
另外,您需要用反斜杠轉義"
,因為它在Java中具有特殊含義。.因此,這是您的正則表達式,可以實現所需的功能:
"{test}".replaceAll("[{}]", "\"")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.