代码之家  ›  专栏  ›  技术社区  ›  Even Mien

从.NET中的字符串中删除双引号

  •  71
  • Even Mien  · 技术社区  · 17 年前

    当前:

    <input type="hidden">
    

    目标是:

    <input type=hidden>
    

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

    s=s。替换(“”,“”);

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

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

    11 回复  |  直到 17 年前
        1
  •  240
  •   Joey Gumbo    17 年前

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

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

    s = s.Replace("\"", "");
    
        2
  •  39
  •   Laurel Enrique    10 年前

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

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

    应导致不使用引号 "hello" 而不是 ""hello""

        3
  •  27
  •   David    17 年前
    s = s.Replace("\"", "");
    

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

        4
  •  14
  •   Fredrik Mörk    17 年前

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

    …但我很好奇你为什么要这么做?我认为引用属性值是一种很好的做法?

        5
  •  6
  •   Steve Gilham    17 年前
    s = s.Replace("\"",string.Empty);
    
        6
  •  5
  •   Svante Svenson    17 年前

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

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

        7
  •  3
  •   Jake Pearson    17 年前

    你必须用反斜杠避开双引号。

    s = s.Replace("\"","");
    
        8
  •  2
  •   TJC    8 年前

    ……例如:

    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)
    
        9
  •  1
  •   gHeidenreich    17 年前

    s=s.Replace(@“”,“”);

        10
  •  1
  •   user3519062    10 年前

    这对我有用

    //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;
    
        11
  •  0
  •   Mike    17 年前
    s = s.Replace( """", "" )
    

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

        12
  •  0
  •   Walter Verhoeven    6 年前

    因为它是带有“引号”文本标识符的糟糕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('"');