使用替换功能从字符串中删除“ {个字符

时间:2019-08-24 13:12:39

标签: asp.net asp.net-mvc-5

我想删除"{并将其替换为{。以下是我当前正在使用的代码行。

var MyString = DataString.Replace(@""{", "");

以下是我收到的错误消息 enter image description here

请告知

谢谢

2 个答案:

答案 0 :(得分:1)

您需要使用两个引号对要替换的引号进行转义,因此对于您的示例:

    var MyString = DataString.Replace(@"""{", "{");

有关在字符串中使用引号的替代方法,请参见How to include quotes in a string

答案 1 :(得分:1)

如果您希望获得JSON数据,那么您真正需要的是JSON Parser。而且,如果您只想将"{替换为{,则只需转义并替换如下字符串:

// Suppose the variable named str has a value of Hello"{ wrapped in double quotes
var strReplaced = str.Replace("\"{", "{");
Console.WriteLine($"strReplaced: {strReplaced}");
// This will result in strReplaced: Hello{