簡體   English   中英

如何轉義多行文字字符串中的雙引號

[英]How to Escape Double Quote in Multline Literal String

我正在構建一個 HTML 的字符串。我想讓它多行以便我可以閱讀它,但我似乎無法弄清楚如何轉義雙引號。 我正在使用string = @"bla"作為字符串文字以允許多行,但是當我似乎無法使用反斜杠來轉義雙引號時。

這是我所擁有的...

string output;

output = @"<html>
    <head>
        <style>
            th, tr, td { padding-left: 5px; }
        </style>    
    </head>
    <body>
        <table>
            <tr>
                <td colspan=\"2\">";

它掛在<td colspan=\"2\">上 我可以不將 \ 與文字一起使用嗎? 我有哪些選擇?

您只需使用 2 個連續的雙引號 ( "" ):

string s = @"Porky The Pig said, ""That's all, Folks!""...";

但是您可能想要使用合適的模板引擎,例如(但絕不限於):

與滾動你自己的相比,它們幾乎肯定不會受到各種注入攻擊的影響。

您可能會發現在 HTML 文件中創建 HTML 比連接字符串更可取。 嘗試以下操作:

創建一個 Windows Forms App (.NET Framework)

對比 2019

打開解決方案資源管理器

  • 在 VS 菜單中,單擊查看
  • Select解決方案資源管理器

打開屬性 Window

  • 在 VS 菜單中,單擊查看
  • Select屬性 Window

添加文件夾(名稱:Html)

  • 在解決方案資源管理器中,右鍵單擊 <project name>
  • Select添加
  • Select新建文件夾 重命名為所需名稱(例如:Html)

添加 HTML 文件

  • 在解決方案資源管理器中,右鍵單擊您創建的文件夾(例如:Html)
  • Select添加
  • Select新品...
  • 在左側的Visual C# Items下,單擊Web
  • Select HTML 頁面(名稱:HTMLPage1.html)
  • 點擊添加
  • 在解決方案資源管理器中,單擊 HTML 頁面(例如:HTMLPage1.html)
  • 在 Properties Window 中,設置以下屬性: Build Action: Embedded Resource

創建一個class (名稱:HelperLoadResource.cs)

添加以下使用語句

  • using System;
  • using System.Text;
  • using System.IO;
  • using System.Reflection;

HelperLoadResource

注意:以下代碼來自這篇文章

public static class HelperLoadResource
{
    public static string ReadResource(string filename)
    {
        //use UTF8 encoding as the default encoding
        return ReadResource(filename, Encoding.UTF8);
    }

    public static string ReadResource(string filename, Encoding fileEncoding)
    {
        string fqResourceName = string.Empty;
        string result = string.Empty;

        //get executing assembly
        Assembly execAssembly = Assembly.GetExecutingAssembly();

        //get resource names
        string[] resourceNames = execAssembly.GetManifestResourceNames();

        if (resourceNames != null && resourceNames.Length > 0)
        {
            foreach (string rName in resourceNames)
            {
                if (rName.EndsWith(filename))
                {

                    //set value to 1st match
                    //if the same filename exists in different folders,
                    //the filename can be specified as <folder name>.<filename>
                    //or <namespace>.<folder name>.<filename>
                    fqResourceName = rName;

                    //exit loop
                    break;
                }
            }

            //if not found, throw exception
            if (String.IsNullOrEmpty(fqResourceName))
            {
                throw new Exception($"Resource '{filename}' not found.");
            }

            //get file text
            using (Stream s = execAssembly.GetManifestResourceStream(fqResourceName))
            {
                using (StreamReader reader = new StreamReader(s, fileEncoding))
                {
                    //get text
                    result = reader.ReadToEnd();
                }
            }
        }

        return result;
    }
}

用法

//get HTML as string
string html = HelperLoadResource.ReadResource("HTMLPage1.html");

資源

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM