使用Console.WriteLine()
,输出:
我希望它自动显示,而不是在需要的地方手动输入\n
:
这可能吗?如果是这样,怎么样?
答案 0 :(得分:5)
这是一个可以使用制表符,换行符和其他空格的解决方案。
exchange: {
source_currency: payload.base_currency,
target_currency: payload.meta.effective_params.quote_currencies[0],
value: payload.quotes[payload.meta.effective_params.quote_currencies[0]].midpoint
}
答案 1 :(得分:4)
这将采用一个字符串并返回一个不超过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.
答案 2 :(得分:2)
在几分钟内编码,它实际上会破坏超过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和内存方面也没有得到很好的优化。我不会在任何严肃的背景下使用它。
答案 3 :(得分:0)
这应该有用,虽然它可能会缩短一些:
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]);
}
}
可能很难理解,所以基本上是这样做的:
Trim()
删除开头和结尾的空格
Regex()
用一个空格替换多个空格
for
循环从段落中获取第一个(Console.WindowWidth - 1)字符并将其设置为新行。
`LastIndexOf()1试图找到该行中的最后一个空格。如果没有,则按原样离开
该段从段落中删除,循环重复。
注意:正则表达式取自here。 注2:我不认为它取代了标签。
答案 4 :(得分:0)
您可以使用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由我开发。
答案 5 :(得分:0)
如果您的单词(例如路径)大于屏幕宽度,则还需要自动换行。
using System;
using System.Text;
namespace Colorify.Terminal
{
public static class Wrapper
{
static int _screenWidth { get; set; }
public static void Text(string text)
{
StringBuilder line = new StringBuilder();
string[] words = text.Split(' ');
_screenWidth = (Console.WindowWidth - 3);
foreach (var item in words)
{
Line(ref line, item);
Item(ref line, item);
}
if (!String.IsNullOrEmpty(line.ToString().Trim()))
{
Out.WriteLine($"{line.ToString().TrimEnd()}");
}
}
static void Line(ref StringBuilder line, string item)
{
if (
((line.Length + item.Length) >= _screenWidth) ||
(line.ToString().Contains(Environment.NewLine))
)
{
Out.WriteLine($"{line.ToString().TrimEnd()}");
line.Clear();
}
}
static void Item(ref StringBuilder line, string item)
{
if (item.Length >= _screenWidth)
{
if (line.Length > 0)
{
Out.WriteLine($" {line.ToString().TrimEnd()}");
line.Clear();
}
int chunkSize = item.Length - _screenWidth;
string chunk = item.Substring(0, _screenWidth);
line.Append($"{chunk} ");
Line(ref line, item);
item = item.Substring(_screenWidth, chunkSize);
Item(ref line, item);
}
else
{
line.Append($"{item} ");
}
}
}
}
它已在Colorify-具有文本格式的C#NETCore控制台库中实现:颜色,对齐方式以及更多[适用于Win,Mac和Linux]
答案 6 :(得分:-1)
您应该能够利用Console.WindowWidth使用一些前瞻性的换行逻辑来实现这一目标。