使Console.WriteLine()包装单词而不是字母

使用Console.WriteLine() ,它输出:

在此处输入图像描述

我想让它看起来像这样,而不是在需要的地方手动放入\n

在此处输入图像描述

这可能吗? 如果是这样,怎么样?

这应该有效,尽管它可能会缩短一些:

  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(); 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]); } } 

可能很难理解,所以基本上这样做:

Trim()删除开头和结尾的空格。
Regex()用一个空格替换多个空格。
for循环从段落中获取第一个(Console.WindowWidth – 1)字符并将其设置为新行。
`LastIndexOf()1试图找到该行的最后一个空格。 如果没有,它就会保留原样。
该段从段落中删除,循环重复。

注意:正则表达式是从这里获取的 。 注2:我不认为它取代了标签。

这是一个可以使用制表符,换行符和其他空格的解决方案。

 using System; using System.Collections.Generic; ///  /// Writes the specified data, followed by the current line terminator, to the standard output stream, while wrapping lines that would otherwise break words. ///  /// The value to write. /// The value that indicates the column width of tab characters. 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 wrapped = new List(); 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); } } 

这将采用一个字符串并返回一个字符串列表,每个字符串不超过80个字符):

 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; }); 

从这个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."; 

我得到这个结果:

 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. 

在几分钟内编码,它实际上将打破超过80个字符的单词并且不考虑Console.WindowWidth

 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()); } 

它在CPU和内存方面也没有得到很好的优化。 我不会在任何严肃的背景下使用它。

您可以使用CsConsoleFormat †通过自动换行将字符串写入控制台。 它实际上是默认的文本换行模式(但它可以更改为字符换行或不换行)。

 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); 

您还可以拥有一个包含数字余量的实际列表:

 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); 

这是它的样子:

†CsConsoleFormat是我开发的。

您可能应该能够利用Console.WindowWidth和一些前瞻性的换行逻辑来实现这一点。