在C#中使用stringbuilder在双引号后面附加字符

Pri*_*nda -2 c# asp.net stringbuilder

嗨,我想补充一下

<%@页面语言=“ C#” AutoEventWireup =“ true” CodeBehind =“ WebForm1.aspx.cs” Inherits =“ WebApplication1.WebForm1”%>

到文本文件,但是我被卡在“ C#”,“ WebForm1.aspx.cs”和“ WebApplication1.WebForm1”部分,我的代码如下

 public string HeaderContent1()
 {
     var sb = new StringBuilder();
     sb.Append("<%@ Page Language=");
     sb.Append("c#");// prints only c#
     sb.Append("AutoEventWireup=");
     sb.Append("true");
     sb.Append("CodeBehind=");
     sb.Append("WebForm1.aspx.cs");// prints only WebForm1.aspx.cs I want with double quotes
     sb.Append("WebApplication1.WebForm1");// prints only WebApplication1.WebForm1 I want with double quotes
     sb.Append("%>");

     sb.AppendLine();
     sb.Append("<html>");
     sb.AppendLine();// which is equal to Append(Environment.NewLine);
     sb.Append("<head>");
     sb.AppendLine();
     sb.Append("<h1>New File header</h1>");
     sb.AppendLine(); // which is equal to Append(Environment.NewLine);
     sb.Append("</head>");
     sb.AppendLine();
     sb.Append("<body>");
     sb.AppendLine(); // which is equal to Append(Environment.NewLine);
     sb.Append("<h2> New File Body</h2>");
     sb.AppendLine();

     sb.Append("</body>");
     sb.AppendLine();

     sb.Append("</html>");
     sb.AppendLine();
}
Run Code Online (Sandbox Code Playgroud)

Gra*_*ICA 5

尝试带引号的字符串中使用引号时,有两种选择。

您可以转义报价:

sb.Append("\"WebForm1.aspx.cs\"");
Run Code Online (Sandbox Code Playgroud)

或使用逐字符号:

sb.Append(@"""WebForm1.aspx.cs""");
Run Code Online (Sandbox Code Playgroud)

您可以StringBuilder完全消除:

var page = "<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"WebForm1.aspx.cs\" Inherits=\"WebApplication1.WebForm1\" %><html>\r\n<head>\r\n<h1>New File header</h1>\r\n</head>\r\n<body>\r\n<h2> New File Body</h2>\r\n</body>\r\n</html>";
Run Code Online (Sandbox Code Playgroud)