String

S

shapper

Hello,

I am using replacing a parameter into a string as follows:

string.Format("var Tracker = getTracker(\"{0}\");", account));

If account is, for example, 123I get:
var Tracker = getTracker("123");

But if account is null I get:
var Tracker = getTracker("");

In this case I would like to get this without the quotes. Just:
var Tracker = getTracker();

Can I do this without the need to wrap my code line inside an "if" ?

Thanks,
Miguel
 
J

Jon Skeet [C# MVP]

I am using replacing a parameter into a string as follows:

string.Format("var Tracker = getTracker(\"{0}\");", account));

If account is, for example, 123I get:
var Tracker = getTracker("123");

But if account is null I get:
var Tracker = getTracker("");

In this case I would like to get this without the quotes. Just:
var Tracker = getTracker();

Can I do this without the need to wrap my code line inside an "if" ?

Yes:
string.Format("var Tracker = getTracker({0});",
account == null ? "" : "\"" + account + "\"")

Jon
 
J

Jon Skeet [C# MVP]

I am using replacing a parameter into a string as follows:

string.Format("var Tracker = getTracker(\"{0}\");", account));

If account is, for example, 123I get:
var Tracker = getTracker("123");

But if account is null I get:
var Tracker = getTracker("");

In this case I would like to get this without the quotes. Just:
var Tracker = getTracker();

Can I do this without the need to wrap my code line inside an "if" ?

Yes:
string.Format("var Tracker = getTracker({0});",
account == null ? "" : "\"" + account + "\"")

Jon
 

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