It is required to display a flat table in the console. Currently decided this with:

Console.Write("{0, -20}|", text); 

With this code I set the minimum number of characters in a column, but if there are> 20 characters, everything will break. How to cut the output if the string is more than 20 characters?

  • In what sense does "trim"? - Ev_Hyper
  • Console.Write("{0, -20}|", text.Substring(0,17) + "..."); ? - Ev_Hyper
  • It is better, of course, to render a special function or even an extension method, for beauty. - Ev_Hyper
  • Console.Write ("{0, -20} |", element.Value.Substring (0.17) + "..."); Throws Exception "The index and length should indicate the position in the string." Where is the mistake? - Maxim Shinkarev
  • So you have a string length less than 17 characters, now I will write an example, wait. - Ev_Hyper

3 answers 3

In two steps, of course.

Collect all lines of output, find out the maximum length. Based on the maximum length to form a format string. Output through it.

In your case, instead of output, you will have

 var column1 = new List<string>(); 
 column1.Add(text); 

And when all the lines are added,

 var maxWidth = column1.Max(s => s.Length); var formatString = string.Format("{{0, -{0}}}|", maxWidth); foreach (var s in column1) { Console.Write(formatString, s); // тут наверное остальные колонки Console.WriteLine(); } 
  • Bad option. If there is a line in the list, say ... in 100 characters, then the output will be appropriate. - Ev_Hyper
  • @Ev_Hyper: Well, the maximum width of the columns should also be set, of course. But to define a column 20 wide a priori is somehow too easy and wrong. - VladD
  • Yes, with 100+ characters it will look bad. The option is good, but it is not suitable for mine. - Maxim Shinkarev
  • @MaximShinkarev: Then Ev_Hyper, perhaps, will give an alternative answer :) - VladD
  • the fixed column width for the console is the most The limit for the total output width is given in characters. Your option for console menus is good, I use it myself so that the width of the "cursor" (background color selection) does not jump - rdorn

You can write an auxiliary extension method:

 public static class StringHelper { public static string Shorten(this string str, int value) { return value > 3 && str.Length > value ? str.Substring(0, value - 3) + "..." : str; } } 

which will add ellipsis if the length of the string is greater than value .

Example of use:

 Console.Write("{0, -20} ", text.Shorten(20)); 
  • Then the nested ternary operator :) - VladD
  • @VladD yes there is no need for any nesting - I was in a hurry, and added a condition “on the fly” I'll fix it now. - Ev_Hyper
  • Too not perfect, so, IMHO, it is better to throw an exception if the value is less than 3. - Ev_Hyper
  • You can use the unicode symbol "ellipsis", it is, in theory, takes only one familiarity. But whether it is displayed in console font, this is a question. - VladD
  • one
    when shortening long file names for compatibility with DOS (8.3 format) used ~ , quite a good option for console IMHO - rdorn

Solved the problem this way:

 if (element.Value.Length >= 20) { Console.Write("{0, -20}|", element.Value.Substring(0, 17) + "..."); } else { Console.Write("{0, -20}|", element.Value); } 

Thanks @Ev_Hyper for the help!