I have 3 strings, and I want to put them in a Label on 3 lines. The label is crammed in, and needs to be a fixed height, so I only want each string to take up 1 line. That said, I want to keep length of each string to ~20 characters. The problem is, I get an exception thrown is the string is < 20 characters, so I need to evaluate whether it is or not BEFORE writing it to the label.
What I tried was:
mobPartLabel.Text = String.Format(
"String 1: {0} \nString 2: {1} \nString 3: {2}",
(string1.Length >= 20 ? string1.Substring(0, 19) : string1),
(string2.Length >= 20 ? string2.Substring(0, 19) : string2),
(string3.Length >= 20 ? string3.Substring(0, 19) : string3));
Obviously, this didn't work. :-\ But it's basically what I would like to achieve. Please forgive this noobish question; I'm new to using tertiary operators. I know what they are and what they do, but I have never found a reason to implement one until now >_>
Does anyone have any suggestions on what I might be doing wrong here, or if it is completely impossible, what would be a better method?
EDIT Using this code
string string1 = "Rawr";
string string2 = "Rawr2";
string string3 = "Rawr3";
mobPartLabel.Text = String.Format(
"String 1: {0} \nString 2: {1} \nString 3: {2}",
(string1.Length >= 20 ? string1.Substring(0, 19) : string1),
(string2.Length >= 20 ? string2.Substring(0, 19) : string2),
(string3.Length >= 20 ? string3.Substring(0, 19) : string3));
All my label says is "String 1:" and that's it. That's the full and complete label. :(