简体   繁体   English

用双引号替换字符串

[英]replace string with double quotes

I want to replace, from a JSON file :"[" to :[" 我想从JSON文件替换:“[”to:[“

The below runs but without delivering the expected.Any clues?(I looked in similar questions, but I was more confused) 以下运行,但没有提供预期。任何线索?(我看了类似的问题,但我更困惑)

string contenty = contentx.Replace(":"["",":["");
return contentx;

You're returning contentx instead of contenty . 你返回的是contentx而不是contenty contenty is the variable that has the new string. contenty是具有新字符串的变量。

First you have to escape the double quotes with \\" 首先,您必须使用\\“转义双引号

Then you have to return the "return value" of the expression in the same variable, or simply use one return statement: 然后你必须在同一个变量中返回表达式的“返回值”,或者只使用一个return语句:

return contentx.Replace(":\"[\"", ":[\"");

Try it like this (you have some issues with the double quotes in a string): 试试这样(你在字符串中的双引号有一些问题):

return contentx.Replace(@":""[""", @":[""");

Another option is: 另一种选择是:

return contentx.Replace(":\"[\"", ":[\"");

This will make sure that the character escaping goes well and your string is replaced properly. 这将确保字符转义顺利并且您的字符串被正确替换。 Moreover, as Equalsk showed in his comment, this will also solve the problem of returning the wrong variable and creating an unnecessary variable. 而且,正如Equalsk在他的评论中所表明的那样,这也将解决返回错误变量和创建不必要变量的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM