String Builder

S

shapper

Hello,

I am appending a string to a StringBuilder instance as follows:
paper.AppendFormat("<div id=\"{0}\" class=\"{1}\"></div>", id,
cssClass);

However I would like to add id=\"{0}\" only if "id" is not null or
empty and the same for cssClass and class=\"{1}\".

Is there a conditional way to do this?

I was trying something like
paper.AppendFormat("<div id=\"{0}\" class=\"{1}\"></div>", new object
[] { ... });

with some conditions but I am not getting anywhere.

Thanks,
Miguel
 
P

Peter Duniho

Hello,

I am appending a string to a StringBuilder instance as follows:
paper.AppendFormat("<div id=\"{0}\" class=\"{1}\"></div>", id,
cssClass);

However I would like to add id=\"{0}\" only if "id" is not null or
empty and the same for cssClass and class=\"{1}\".

Is there a conditional way to do this?

I would just use an if() statement:

paper.Append("<div ");
if (id != null)
{
paper.AppendFormat("id=\"{0}\" ", id);
}
if (cssClass != null)
{
paper.AppendFormat("class=\"{0}\" ", cssClass);
}
paper.Append("></div>"); // or better IMHO: paper.Append("/>");

Alternatively, you can separate the formatting of the object from the
formatting of the string (which still requires if() statements, but might
provide more flexibility in the construction of the string):

string strId = "", strClass = "";

if (id != null)
{
strId = string.Format("id=\"{0}\" ", id);
}
if (cssClass != null)
{
strClass = string.Format("class=\"{0}\" ", cssClass);
}
paper.AppendFormat("<div {0}{1}></div>", strId, strClass);

You could, of course, take that second approach and instead of using local
variables and if() statements, replace the variable uses in the final
AppendFormat() call with ternary operator expressions that are equivalent:

paper.AppendFormat("<div {0}{1}></div>",
id != null ? string.Format("id=\"{0}\" ", id) : "",
cssClass != null ? string.Format("class=\"{0}\" ", cssClass) : "");

There are lots of other approaches, including a more general-purpose
solution that involves a more robust custom formatting method that
extracts key/value pairs from a dictionary, matching the keys to
formatting strings and inserting them as necessary. But that's probably
overkill given the scope of your specific question. :)

Pete
 
H

henry.lee.jr

Hello,

I am appending a string to a StringBuilder instance as follows:
paper.AppendFormat("<div id=\"{0}\" class=\"{1}\"></div>", id,
cssClass);

However I would like to add id=\"{0}\" only if "id" is not null or
empty and the same for cssClass and class=\"{1}\".

Is there a conditional way to do this?

Thanks,
Miguel

Based on your code this is obviously for HTML generation. In my
experience, there is no harm in saying <div id="" class=""></div>, so
here is what I propose (assuming id and cssClass are string objects):

paper.AppendFormat("<div id=\"{0}\" class=\"{1}\"></div>", formatString
(id), formatString(cssClass));

....

private string formatString(string input)
{
if(input==null){ return(""); }
return(input);
}


Alternatively, you could create something like this:

paper.AppendFormat("<div {0} {1}></div>", writeHTMLValue("id", id),
writeHTMLValue("class", cssClass));

and then your function would be:

private string writeHTMLValue(string key, string val)
{
if(val==null){ return(""); }
return("{0}=\"{1}\"", key, id);
}

As the previous poster said, there are many different ways you could
do this of course.
 
P

Peter Duniho

Based on your code this is obviously for HTML generation. In my
experience, there is no harm in saying <div id="" class=""></div>, [...]

Minor caveat that may or may not apply to the OP: simply _having_ an
attribute may affect the behavior of certain parsers of the HTML. There
should be no difference in rendering, but that's not necessarily the only
context in which the HTML may be parsed.

It's not a bad idea...one just needs to make sure that if they are
changing the design goal for the code, that the new design goal is
compatible with the original one, such as it may be.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top