如何将C#变量附加到字符串

时间:2011-05-10 07:49:05

标签: c# string

这是我的代码:

string abc = "hello welcome!!";
StringBuilder sb = new StringBuilder();

sb.Append("<td style='padding-left:30px;width:100%'><span style='text-decoration:none;cursor:pointer' id='AnnMsg' target='_top' ><B><nobr>***"<%= i want to pass abc here %>"***</nobr></B></span></td>");

sb.Append("</tr></table>");

5 个答案:

答案 0 :(得分:3)

sb.Append("<td style='padding-left:30px;width:100%'><span style='text-decoration:none;cursor:pointer' id='AnnMsg' target='_top' ><B><nobr>***"+ abc +"***</nobr></B></span></td>");
sb.Append("</tr></table>");

答案 1 :(得分:3)

你应该像这样使用+ operator

string abc = "hello welcome!!";

StringBuilder sb = new StringBuilder();

sb.Append("<td style='padding-left:30px;width:100%'><span style='text-decoration:none;cursor:pointer' id='AnnMsg' target='_top' ><B><nobr>***" + abc + "***</nobr></B></span></td>");

sb.Append("</tr></table>");

或者你也可以使用string.Format

string abc = "hello welcome!!";

StringBuilder sb = new StringBuilder();

sb.Append(string.Format("<td style='padding-left:30px;width:100%'><span style='text-decoration:none;cursor:pointer' id='AnnMsg' target='_top' ><B><nobr>***{0}***</nobr></B></span></td>", abc));

sb.Append("</tr></table>");

答案 2 :(得分:1)

使用Append方法:

    string abc = "hello welcome!!";

    StringBuilder sb =
      new StringBuilder();
      .Append("<td style='padding-left:30px;width:100%'><span style='text-decoration:none;cursor:pointer' id='AnnMsg' target='_top' ><B><nobr>***")
      .Append(abc)
      .Append("***</nobr></B></span></td>")
      .Append("</tr></table>");

如果字符串可以包含任何需要编码的内容:

      .Append(HttpUtility.HtmlEncode(abc))

答案 3 :(得分:0)

如下:

sb.Append("<td style='padding-left:30px;width:100%'><span style='text-decoration:none;cursor:pointer' id='AnnMsg' target='_top' ><B><nobr>***"+ HttpUtility.HtmlEncode(abc) +"***</nobr></B></span></td>");
sb.Append("</tr></table>");

请注意对HtmlEncode的调用 - 如果您的字符串来自不受信任的来源,这一点非常重要。

答案 4 :(得分:0)

您可以使用更清晰的XML编写器:

StringBuilder builder = new StringBuilder();

using (XmlWriter writer = XmlWriter.Create(builder))
{
    string abc = "hello welcome!!";

    writer.WriteStartElement("td");
    writer.WriteAttributeString("style", "padding-left:30px;width:100%");
    {
        writer.WriteStartElement("span");
        writer.WriteAttributeString("id", "AnnMsg");
        writer.WriteAttributeString("target", "_top");
        writer.WriteAttributeString("style", "text-decoration:none;cursor:pointer");
        {
            writer.WriteStartElement("B");
            {
                writer.WriteStartElement("nobr");
                {
                    writer.WriteString(abc); // Here's where your variable is rendered as text
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
        }
        writer.WriteEndElement();
    }
    writer.WriteEndElement();
}

这是XmlWriter会将XML文本写入StringBuilder。

另一种方法是使用StringBuilder.AppendFormat:

    string abc = "hello welcome!!";

    StringBuilder sb = new StringBuilder();

    sb.AppendFormat
    (
        "<td style='padding-left:30px;width:100%'><span style='text-decoration:none;cursor:pointer' id='AnnMsg' target='_top' ><B><nobr>{0}</nobr></B></span></td></tr></table>",
        arg0: abc
    );

StringBuilder.AppendFormat类似于String.Format:

对我来说,使用XML编写器的主要好处是可以避免人为错误,毫无疑问,您的(X)HTML格式正确。