1

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='AnnMsg' target='_top' ><B><nobr>***"<%= i want to pass abc here %>"***</nobr></B></span></td>");

sb.Append("</tr></table>");
1
  • Remember to accept answers which helped you, (use the tick mark against the answer you found appropriate). Commented May 10, 2011 at 9:39

5 Answers 5

4

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>");
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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

Comments

0

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.

Comments

0

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:

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.