【问题标题】:Use Replace function to remove "{ characters from string使用 Replace 函数从字符串中删除 "{ 字符
【发布时间】:2019-08-24 13:12:39
【问题描述】:

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

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

以下是我收到的错误消息

请指教

谢谢

【问题讨论】:

  • 我相信这不仅仅是任何字符串,而是来自某个地方的 Json 响应。所以这真的是你想要的吗?或者这只是一个快速修复

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


【解决方案1】:

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

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

另请参阅How to include quotes in a string,了解在字符串中使用引号的替代方法。

【讨论】:

    【解决方案2】:

    如果您期待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{
    

    【讨论】:

      猜你喜欢
      • 2011-02-26
      • 2013-09-30
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多