开发者

How to append a C# variable to a string

开发者 https://www.devze.com 2023-03-04 23:21 出处:网络
Here\'s my code: string abc = \"hello welcome!!\"; StringBuilder sb = new StringBuilder(); sb.Append(\"<td style=\'padding-left:30px;width:100%\'><span style=\'text-decoration:none;cursor:p

Here's my code:

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='A开发者_如何学PythonnnMsg' target='_top' ><B><nobr>***"<%= i want to pass abc here %>"***</nobr></B></span></td>");

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


These days i would recommend you use the interpolated string(this is available since C# 6) :

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>");

If you use the interpolated string it is also still possible to use the @ symbol(verbatim identifier) to escape characters. You can do this by adding the @ in front or behind the $.

Alternatively you could use the + operator like this :

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>");

Or you could also use 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>");


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>");


Use the Append method:

    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>");

If the string can contain anything that needs encoding:

      .Append(HttpUtility.HtmlEncode(abc))


As follows:

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>");

Notice the call to HtmlEncode - that's rather important if your string is from an untrusted source.


You can use an XML writer instead which is more cleaner:

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();
}

That's XmlWriter will write XML text to your StringBuilder.

Another approach would be using 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 is like String.Format:

  • http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.appendformat.aspx

For me, the main benefit of using XML writer is you avoid human errors and your (X)HTML will be well-formed with no doubt.

0

精彩评论

暂无评论...
验证码 换一张
取 消