Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding single line if statement in string.format

Can you set a condition inside the string.format parameter.

So if i have

string.format("{0}" , if x = 7 then return "SEVEN" else return "ZERO")

Is there a way of doing this?

like image 296
redoc01 Avatar asked Jan 18 '23 00:01

redoc01


1 Answers

With a ternary operator in VB.Net:

String.Format("{0}", If(x = 7, "SEVEN", "ZERO"))

Same in C# (as Brad already posted):

String.Format("{0}", x == 7 ? "SEVEN" : "ZERO")
like image 177
Dennis Traub Avatar answered Jan 30 '23 06:01

Dennis Traub