[英]Replace partial string with special characters in
I want to replace: reference1\":\" null\"
with reference1\": null
我想用reference1\": null
替换: reference1\":\" null\"
Example part of string to edit:要编辑的字符串的示例部分:
{\"reference1\":\"null\",\"secondarything\":
I've tried literal:我试过文字:
strValue = strValue.Replace(@reference1\":\" null\","reference1\": null");
I have quotes elsewhere I which to keep.我在别处有引用我要保留的地方。 It's the combination of quotation and backslash that has stumped me here.这是引号和反斜杠的组合让我在这里感到困惑。
You need to escape the double quotes using \
-您需要使用\
- 转义双引号
strValue = strValue.Replace("reference1\":\"null\"" , "reference1\": null");
If your string already contains \
, then escape them with additional \
, so now the code looks like -如果您的字符串已经包含\
,那么用额外的\
转义它们,所以现在代码看起来像 -
strValue = strValue.Replace("reference1\\\":\\\"null\\\"", "reference1\\\": null");
Either use a normal string, and add a backslash before each "
or \
:使用普通字符串,并在每个"
或\
之前添加一个反斜杠:
strValue.Replace("reference1\\\":\\\" null\\\"", "reference1\\\": null")
Or use a verbatim string, and double up the "
's:或使用逐字字符串,并将"
加倍:
strValue.Replace(@"reference1\"":\"" null\""", @"reference1\"": null")
That said, this looks like an XY problem: that string looks awfully like JSON, and trying to string manipulation on JSON is normally a bad idea.也就是说,这看起来像一个 XY 问题:该字符串看起来非常像 JSON,并且尝试对 JSON 进行字符串操作通常是个坏主意。 Using a json parser like Json.net is probably a better route towards whatever problem you're trying to solve.使用 Json.net 之类的 json 解析器可能是解决您要解决的任何问题的更好途径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.