1

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. :(

C Smith
  • 756
  • 14
  • 30
  • 6
    "this didn't work" doesn't really give us much detail. I note that your final `string2` should be `string3` though.... – Jon Skeet Apr 29 '13 at 16:14
  • 1
    Additionally, when formatting your code for SO, please use multiple lines to avoid having to scroll horizontally. – Jon Skeet Apr 29 '13 at 16:14
  • 1
    I don't see a problem with what you did, except you have string2 at the end where you mean to have string3. You say it didn't work; what exactly is the problem? – antlersoft Apr 29 '13 at 16:15
  • 2
    What's the exception? – David S. Apr 29 '13 at 16:17
  • 3
    Formatting of the code by Captain Skyhawk likely included fix (using correct variable at the end). Consider making "CutTo20Characters" function to avoid this in the future... – Alexei Levenkov Apr 29 '13 at 16:19
  • Many apologies! As for the "it didn't work", it simply isn't showing up on my Label. >_> I will update my question! – C Smith Apr 29 '13 at 16:27

2 Answers2

3

Your code works just fine. Probably something with a property / size of the label itself. Have you set the AutoSize property to false?

Koen
  • 2,451
  • 1
  • 29
  • 42
  • That was it! I'm sorry, everyone! I didn't even consider that! It cut off at such an location as to make it appear that it wasn't printing past the {0}! – C Smith Apr 29 '13 at 16:37
3

Also, this may be adding unneeded complexity as you seem to be new to .NET and C#, but using the LINQ extension methods on the Enumerable type provided by string, we can actually simplify this further I believe and make the following change.

using System.Linq;
{... other usings and namespace/type declarations ...}

        string string1 = "Rawr";
        string string2 = "Rawr2";
        string string3 = "Rawr3";

        mobPartLabel.Text = String.Format(
            "String 1: {0} \nString 2: {1} \nString 3: {2}",
               (string1.Take(20)),
               (string2.Take(20)),
               (string3.Take(20)));

Arguably this could be made even simpler using Enumerable.Aggregate() but that would not be a friendly piece of code to introduce to you. The important take away here is that the Take method is not naturally part of the string class, but rather is an extension method Enumerable.Take(...) that was added after the class's design was sealed to add additional syntactic sugar for developers.

Linq extends several BCL types (most notably Enumerable) with extension methods designed to enable fluent query expressions using lambda expressions. Linq is not for the faint hearted to pick up and use with no C# experience, but it is becoming rapidly commonplace as are lambda expressions, so learning to recognize their usage early on is important. Consider yourself ready to learn them when you understand delegates, interfaces, and specifically IEnumerable and its special relationship with the C# language (for example, foreach, and yield)

Firoso
  • 6,365
  • 10
  • 42
  • 90
  • +1 for the short syntax and readability but with this approach he has to be sure that the strings are not null. And you miss an extra ')' at the end. ;-) – Koen Apr 29 '13 at 16:55
  • His code will fail as well assuming the strings are null. Null checking is not related to this problem domain, but should be restricted to upstream value validation and sanity checking. – Firoso Apr 29 '13 at 16:57
  • 1
    True. You can disregard my remark. Also good extra explanation on the subject. – Koen Apr 29 '13 at 17:01
  • I did try this, and got something like System.Linq.Enumerable+d__ It went off the screen from there, so I couldn't see the rest. The only alternative I have seen it to make each one a new string. Did I do something wrong? – C Smith Apr 29 '13 at 20:37
  • No error. That's just what mobPartLabel.Text was set to, and was displayed on the form when executing the code above exactly as it is. – C Smith Apr 29 '13 at 21:43
  • Oh yeah interesting, that would have come out as an enumerable sequence of characters which is not a string actually *laughs* http://stackoverflow.com/questions/6083687/how-to-take-on-a-string-and-get-a-string-at-the-end This just became more complicated and somewhat eliminates the usefulness of my answer. – Firoso Apr 30 '13 at 17:16