Make Console.WriteLine () instead of words instead of words

Using Console.WriteLine() , it outputs:

enter image description here

I want this to look so automatic, instead of manually inserting \n wherever you need:

enter image description here

Is it possible? If so, how?

+6
source share
6 answers

This should work, although it can probably be reduced further:

  public static void WordWrap(string paragraph) { paragraph = new Regex(@" {2,}").Replace(paragraph.Trim(), @" "); var left = Console.CursorLeft; var top = Console.CursorTop; var lines = new List<string>(); for (var i = 0; paragraph.Length > 0; i++) { lines.Add(paragraph.Substring(0, Math.Min(Console.WindowWidth, paragraph.Length))); var length = lines[i].LastIndexOf(" ", StringComparison.Ordinal); if (length > 0) lines[i] = lines[i].Remove(length); paragraph = paragraph.Substring(Math.Min(lines[i].Length + 1, paragraph.Length)); Console.SetCursorPosition(left, top + i); Console.WriteLine(lines[i]); } } 

This can be hard to understand, so basically what it does:

Trim() removes spaces at the beginning and end.
Regex() replaces multiple spaces with a single space. The for loop takes the first (Console.WindowWidth - 1) character from the paragraph and sets it as a new line.
`LastIndexOf () 1 tries to find the last space in the string. If he is not there, he leaves the line as it is.
This line is removed from the paragraph and the loop repeats.

Note. The regular expression was taken from here . Note 2: I do not think it replaces tabs.

0
source

Here is a solution that will work with tabs, new characters and other spaces.

 using System; using System.Collections.Generic; /// <summary> /// Writes the specified data, followed by the current line terminator, to the standard output stream, while wrapping lines that would otherwise break words. /// </summary> /// <param name="paragraph">The value to write.</param> /// <param name="tabSize">The value that indicates the column width of tab characters.</param> public static void WriteLineWordWrap(string paragraph, int tabSize = 8) { string[] lines = paragraph .Replace("\t", new String(' ', tabSize)) .Split(new string[] { Environment.NewLine }, StringSplitOptions.None); for (int i = 0; i < lines.Length; i++) { string process = lines[i]; List<String> wrapped = new List<string>(); while (process.Length > Console.WindowWidth) { int wrapAt = process.LastIndexOf(' ', Math.Min(Console.WindowWidth - 1, process.Length)); if (wrapAt <= 0) break; wrapped.Add(process.Substring(0, wrapAt)); process = process.Remove(0, wrapAt + 1); } foreach (string wrap in wrapped) { Console.WriteLine(wrap); } Console.WriteLine(process); } } 
+3
source

Encoded in a couple of minutes, it will break with words that have more than 80 characters, and does not take Console.WindowWidth into account

 private static void EpicWriteLine(String text) { String[] words = text.Split(' '); StringBuilder buffer = new StringBuilder(); foreach (String word in words) { buffer.Append(word); if (buffer.Length >= 80) { String line = buffer.ToString().Substring(0, buffer.Length - word.Length); Console.WriteLine(line); buffer.Clear(); buffer.Append(word); } buffer.Append(" "); } Console.WriteLine(buffer.ToString()); } 

It is also not very optimized for both CPU and Memory. I would not use this in any serious context.

+2
source

This will take a line and return a list of lines no longer than 80 characters):

 var words = text.Split(' '); var lines = words.Skip(1).Aggregate(words.Take(1).ToList(), (l, w) => { if (l.Last().Length + w.Length >= 80) l.Add(w); else l[l.Count - 1] += " " + w; return l; }); 

Starting with this text :

 var text = "Hundreds of South Australians will come out to cosplay when Oz Comic Con hits town this weekend with guest stars including the actor who played Freddy Krueger (A Nightmare on Elm Street) and others from shows such as Game of Thrones and Buffy the Vampire Slayer."; 

I get this result:

 Hundreds of South Australians will come out to cosplay when Oz Comic Con hits town this weekend with guest stars including the actor who played Freddy Krueger (A Nightmare on Elm Street) and others from shows such as Game of Thrones and Buffy the Vampire Slayer. 
+2
source

You can use CsConsoleFormat † to write lines for word wrap consoles. This is actually the default text wrapping mode (but it can be changed to a character wrapper or without a wrapper).

 var doc = new Document().AddChildren( "2. I have bugtested this quite a bit however if something " + "goes wrong and the program crashes just restart it." ); ConsoleRenderer.RenderDocument(doc); 

You can also have an actual list with a field for numbers:

 var docList = new Document().AddChildren( new List().AddChildren( new Div("I have not bugtested this enough so if something " + "goes wrong and the program crashes good luck with it."), new Div("I have bugtested this quite a bit however if something " + "goes wrong and the program crashes just restart it.") ) ); ConsoleRenderer.RenderDocument(docList); 

Here's what it looks like:

† CsConsoleFormat was developed by me.

0
source

You will probably be able to use Console.WindowWidth with the new newline logic to make this happen.

-1
source

All Articles