【问题标题】:Strip double quotes from a string in .NET从.NET中的字符串中去除双引号
【发布时间】:2010-11-13 18:05:55
【问题描述】:

我正在尝试匹配一些格式不一致的 HTML,需要去掉一些双引号。

当前:

<input type="hidden">

目标:

<input type=hidden>

这是错误的,因为我没有正确地转义它:

s = s.Replace(""","");

这是错误的,因为没有空白字符(据我所知):

s = s.Replace('"', '');

用空字符串替换双引号的语法/转义字符组合是什么?

【问题讨论】:

    标签: c# .net vb.net


    【解决方案1】:

    如果您想删除单个字符,我想简单地读取数组并跳过该字符并返回数组会更容易。我在自定义解析 vcard 的 json 时使用它。 因为它是带有“引用”文本标识符的坏 json。

    将以下方法添加到包含您的扩展方法的类中。

      public static string Remove(this string text, char character)
      {
          var sb = new StringBuilder();
          foreach (char c in text)
          {
             if (c != character)
                 sb.Append(c);
          }
          return sb.ToString();
      }
    

    然后你可以使用这个扩展方法:

    var text= myString.Remove('"');
    

    【讨论】:

      【解决方案2】:

      如果您只想从字符串的末尾(而不是中间)去掉引号,并且字符串的任一端都有可能存在空格(即解析一个 CSV 格式文件,其中有逗号后的空格),则需要调用Trim函数两次...例如:

      string myStr = " \"sometext\"";     //(notice the leading space)
      myStr = myStr.Trim('"');            //(would leave the first quote: "sometext)
      myStr = myStr.Trim().Trim('"');     //(would get what you want: sometext)
      

      【讨论】:

        【解决方案3】:

        我没有看到我的想法已经重复了,所以我建议您查看 Microsoft C# 文档中的 string.Trim,您可以添加要修剪的字符,而不是简单地修剪空格:

        string withQuotes = "\"hellow\"";
        string withOutQotes = withQuotes.Trim('"');
        

        应该导致 withOutQuotes 为 "hello" 而不是 ""hello""

        【讨论】:

          【解决方案4】:

          这对我有用

          //Sentence has quotes
          string nameSentence = "Take my name \"Wesley\" out of quotes";
          //Get the index before the quotes`enter code here`
          int begin = nameSentence.LastIndexOf("name") + "name".Length;
          //Get the index after the quotes
          int end = nameSentence.LastIndexOf("out");
          //Get the part of the string with its quotes
          string name = nameSentence.Substring(begin, end - begin);
          //Remove its quotes
          string newName = name.Replace("\"", "");
          //Replace new name (without quotes) within original sentence
          string updatedNameSentence = nameSentence.Replace(name, newName);
          
          //Returns "Take my name Wesley out of quotes"
          return updatedNameSentence;
          

          【讨论】:

            【解决方案5】:

            我认为您的第一行实际上可以工作,但我认为您需要四个引号来表示包含单个引号的字符串(至少在 VB 中):

            s = s.Replace("""", "")
            

            对于 C#,您必须使用反斜杠转义引号:

            s = s.Replace("\"", "");
            

            【讨论】:

            • 如果字符串中有更多的嵌入引号会产生副作用
            【解决方案6】:

            s = s.Replace(@"""", "");

            【讨论】:

            • 如果字符串中有更多的嵌入引号会产生副作用
            【解决方案7】:
            s = s.Replace( """", "" )
            

            在字符串中,两个相邻的引号将用作预期的 " 字符。

            【讨论】:

            • 如果字符串中有更多的嵌入引号会产生副作用
            【解决方案8】:

            c#:"\"",因此是s.Replace("\"", "")

            vb/vbs/vb.net:"" 因此s.Replace("""", "")

            【讨论】:

            • 如果字符串中有更多的嵌入引号会产生副作用
            【解决方案9】:

            您可以使用以下任何一种:

            s = s.Replace(@"""","");
            s = s.Replace("\"","");
            

            ...但我确实很好奇您为什么要这样做?我认为引用属性值是一种好习惯?

            【讨论】:

            • 我正在使用 HTML Agility Pack 来查找某个链接,然后我需要从 HTML 文本中删除该链接中的一个值。 HTML 敏捷包引用属性值,但不引用原始 HTML。 (所有这些都是为了一个测试。)
            • 如果字符串中有更多的嵌入引号会产生副作用
            【解决方案10】:
            s = s.Replace("\"",string.Empty);
            

            【讨论】:

            • 如果字符串中有更多的嵌入引号会产生副作用
            【解决方案11】:

            你必须用反斜杠转义双引号。

            s = s.Replace("\"","");
            

            【讨论】:

            • 如果字符串中有更多的嵌入引号会产生副作用
            【解决方案12】:
            s = s.Replace("\"", "");
            

            您需要使用 \ 来转义字符串中的双引号字符。

            【讨论】:

            • 如果字符串中有更多的嵌入引号会产生副作用
            猜你喜欢
            • 2011-01-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-10-09
            • 2012-09-26
            • 1970-01-01
            相关资源
            最近更新 更多