【问题标题】:Double quotes inside string HTML [duplicate]字符串 HTML 中的双引号 [重复]
【发布时间】:2013-04-19 19:53:28
【问题描述】:

我想保存这个:

<span class="icon phone">m</span>

在一个字符串中。我该怎么做?

已尝试:@"&lt;span class="+"icon phone"+"&gt;m&lt;/span&gt;";

已尝试:@"&lt;span class="+@"icon phone"+"&gt;m&lt;/span&gt;";

请帮忙!

【问题讨论】:

  • 连续使用两个双引号:""
  • 你也可以这样逃跑:"&lt;span class=\"icon phone\"&gt;m&lt;/span&gt;"
  • 将其保存在文本文件中。然后从代码中读取该文件的第一行(记得修剪行尾!)。

标签: c# string


【解决方案1】:

您可以通过键入 " 两次来做到这一点。它将出现一次,在 @-string 中。

所以,在你的情况下,存储:

<span class="icon phone">m</span>

你的字符串是:

string s = @"<span class=""icon phone"">m</span>";

【讨论】:

    【解决方案2】:

    改为使用单引号。

    var html = "<span class='icon phone'>m</span>";
    

    或将文字字符串中的引号加倍

    var html = @"<span class=""icon phone"">m</span>";
    

    或者用反斜杠转义引号

    var html = "<span class=\"icon phone\">m</span>";
    

    【讨论】:

    • 单引号在 HTML4 中有效(和 5?不确定),但在 XHTML 中无效...顺便说一句,您的第二个代码 sn-p 末尾有一个额外的单引号
    • 单引号在 XHTML 中有效。修正了错字,谢谢
    • 我的错;我很确定它们在 XML 中是被禁止的(因此在 XHTML 中也是如此),但显然我错了......
    【解决方案3】:

    要将引号保存在字符串中,您必须对其进行屏蔽:

    您可以通过 string mystring = @"&lt;span class=""icon phone""&gt;m&lt;/span&gt;"; 或直接使用反斜杠 (\) string mystring = "&lt;span class=\"icon phone\"&gt;m&lt;/span&gt;"; 来屏蔽引号。

    【讨论】:

      【解决方案4】:

      简单

      String html = "<span class=\"icon phone\">m</span>"
      

      或者你可以使用字符串字面量:

      String html = @"<span class=""icon phone"">m</span>"
      

      就是这样。

      【讨论】:

        【解决方案5】:

        试试这个:

          string str="<span class=\"icon phone\">m</span>";
        

        【讨论】:

          【解决方案6】:

          您也可以省略 @ 并使用反斜杠 \ 转义双引号:

          "<span class=\"icon phone\">m</span>"
          

          【讨论】:

            【解决方案7】:

            怎么样

            new XElement("span", new XAttribute("class", "icon phone"), "m").ToString()    
            

            【讨论】:

              猜你喜欢
              • 2014-09-26
              • 1970-01-01
              • 1970-01-01
              • 2014-09-20
              • 2013-03-14
              • 1970-01-01
              • 2019-06-27
              • 2017-09-08
              • 2017-02-19
              相关资源
              最近更新 更多