[英]Replace partial string with special characters in
我想用reference1\": null
替換: reference1\":\" null\"
要編輯的字符串的示例部分:
{\"reference1\":\"null\",\"secondarything\":
我試過文字:
strValue = strValue.Replace(@reference1\":\" null\","reference1\": null");
我在別處有引用我要保留的地方。 這是引號和反斜杠的組合讓我在這里感到困惑。
您需要使用\
- 轉義雙引號
strValue = strValue.Replace("reference1\":\"null\"" , "reference1\": null");
如果您的字符串已經包含\
,那么用額外的\
轉義它們,所以現在代碼看起來像 -
strValue = strValue.Replace("reference1\\\":\\\"null\\\"", "reference1\\\": null");
使用普通字符串,並在每個"
或\
之前添加一個反斜杠:
strValue.Replace("reference1\\\":\\\" null\\\"", "reference1\\\": null")
或使用逐字字符串,並將"
加倍:
strValue.Replace(@"reference1\"":\"" null\""", @"reference1\"": null")
也就是說,這看起來像一個 XY 問題:該字符串看起來非常像 JSON,並且嘗試對 JSON 進行字符串操作通常是個壞主意。 使用 Json.net 之類的 json 解析器可能是解決您要解決的任何問題的更好途徑。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.