1

If hasSecond == false, I want to write

myString = string.Format(CultureInfo.InvariantCulture, 
      "{0}", 
      "first");

If hasSecond == true, I want to write

myString = string.Format(CultureInfo.InvariantCulture, 
      "{0}{1}", 
      "first", 
      "second");

How can I combine them together?

For example, by using conditional expressions, the following doesn't seem work, because it doesn't make the fourth parameter optional:

myString = string.Format(CultureInfo.InvariantCulture, 
      "{0}" + (hasSecond == true ? "{1}" : string.Empty), 
      "first", 
      hasSecond == true ? "second" ; string.Empty);

when hasSecond==false, it becomes

myString = string.Format(CultureInfo.InvariantCulture, 
      "{0}" + string.Empty, 
      "first", 
      string.Empty);

where we only have one place holder {0}, while have two parameters "first" and string.Empty to fill it in.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
Tim
  • 1
  • 141
  • 372
  • 590
  • 2
    *"The following doesn't seem work"* - what does it mean? Error? Unexpected result? What was unexpected? Note that you have `;` instead of `:` in last case – Sergey Berezovskiy Mar 09 '17 at 20:31
  • actually it does not matter, how many parameters more there are in the end, because they will only appear in the string if they have the right place holder. 1 condition should suffice – Mong Zhu Mar 09 '17 at 20:35
  • @MongZhu: Thanks. Can you be specific? – Tim Mar 09 '17 at 20:36
  • `string.Format("{0}", var1, var2, var3, var4)` --> only var1 will be vissible, the rest not, You need only 1 conditional case that would add a next `{1}` place holder so that var2 can appear, you don't really need the `string.empty` – Mong Zhu Mar 09 '17 at 20:38

3 Answers3

4
myString = string.Format(CultureInfo.InvariantCulture, 
  "{0}{1}", 
  "first", 
  hasSecond ? "second" : string.Empty);

This should work

Liu
  • 970
  • 7
  • 19
1

As I already wrote in my comment the second if condition is not necessary, because the variable will only appear in the string if it has the right place holder.

In this scenario the second variable will not end up in the string:

static void Main(string[] args)
{
    double var1 = 99.9;
    double var2 = 99.1;

    bool condition = false;

    string res = string.Format(CultureInfo.InvariantCulture, "{0}" + (condition ? "{1}" : string.Empty), var1, var2);

    Console.WriteLine(res);

    Console.ReadLine();
}

EDIT:

Comparing the Answer by Jianping Liu:
The only difference that I can see is that if you have a longer string and want to insert the values at certain positions than it might be more convenient to set the condition at the end and leave the second placeholder {1} standing in all cases:

string res = string.Format(CultureInfo.InvariantCulture, 
           "The measurement revealed a value of: {0}{1} bla bla", 
           var1, condition ?  " which strongly depends on the value of: " + var2 : string.Empty);

compare to:

string res = string.Format(CultureInfo.InvariantCulture,
             "The measurement revealed a value of: {0}" +
              (condition ? " which strongly depends on the value of: {1}" : string.Empty) + 
              " bla bla", var1, var2);

But I guess it is a matter of taste which one is more readable for you.

Community
  • 1
  • 1
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • Thanks. Can you compare your method to [Jianping Liu's](http://stackoverflow.com/a/42704872/156458)? – Tim Mar 09 '17 at 20:52
  • @Tim He did almost the same as me, just the other way around. He left the second placeholder in place and removed the second parameter. I guess from the perspective of performance the difference is negligible – Mong Zhu Mar 09 '17 at 20:56
  • @Tim I guess there is a case of readability that might be a difference. Have a look at my edit, I made a comparison – Mong Zhu Mar 10 '17 at 07:37
0
string value = $"{first}{hasSecond ? second : string.Empty}";
Sam Axe
  • 33,313
  • 9
  • 55
  • 89
  • @MongZhu What is the (dis)advantage of using place holder vs no using place holder? – Tim Mar 09 '17 at 20:42
  • @Tim I don't really understand. Do you mean: [When is it better to use String.Format vs string concatenation?](http://stackoverflow.com/questions/296978/when-is-it-better-to-use-string-format-vs-string-concatenation) – Mong Zhu Mar 09 '17 at 20:45
  • @Tim I guess in your case the only real advantage is to get alle variable formated to a string using only one call of `CultureInfo.InvariantCulture`, other wise you could just concatenate them with `+` – Mong Zhu Mar 09 '17 at 20:51