我想在控制台的右侧打印一个合理的文本。就像所有控制台帮助命令一样。
例如,复制命令:通过键入copy /?
结果的一部分将是以下文本(由于Windows语言,该文本为德语):
Quelle Bezeichnet die zu kopierende(n) Datei(en).
/A Weist auf eine ASCII-Textdatei hin.
/B Weist auf eine Binärdatei hin.
/D Zieldatei kann entschlüsselt erstellt werden.
Ziel Bezeichnet das Verzeichnis und/oder Dateinamen der neuen
Datei(en).
/V Überprüft, ob die Dateien richtig geschrieben wurden.
/N Verwendet den Kurznamen (falls vorhanden), beim Kopieren
einer Datei mit einem Nicht-8Punkt3-Namen.
/Y Unterdrückt die Bestätigungsaufforderung beim Überschreiben
vorhandener Zieldateien.
/-Y Fordert beim Überschreiben vorhandener Zieldateien zum
Bestätigen auf.
在这种情况下,“右列”中超出界限的所有文本将在列的开头继续,例如: Ziel
我尝试了以下操作,但没有任何操作返回期望的结果:
Console.WriteLine("{0,10}", "/s The path from the source file (.ini file)");
Console.Write("{0,2}{1,20}", "/d", "The path where the destination file will be converted and copied (.xml file)");
Console.WriteLine("{0}\t\t{1}", "/s=d", "The path from the source file. The destination file will be automatically converted and copied in the same path with the source file.".PadLeft(100));
Console.WriteLine("{0,-10}{1,10}{2,-30}",
"/s The path from the source file (.ini file)",
"/d The path where the destination file will be converted and copied (.xml file)",
"/s=d The path from the source file. The destination file will be automatically converted and copied in the same path with the source file.");
好吧,作为“快速而肮脏”的方法来编写具有左缩进的字符串,您可以执行一些简单的计算,然后“逐个部分”地编写来控制台字符串。
样例代码:
private static void WriteLineIndented(string line, int leftMargin, int width)
{
if (string.IsNullOrEmpty(line))
return;
for(int i = 0; i < leftMargin; i++)
Console.Write(" ");
if (line.Length <= width - leftMargin)
{
Console.WriteLine(line);
return;
}
else
{
int position = Math.Min(width - leftMargin, line.Length - 1);
while (position > 0 && line[position] != ' ')
position--;
Console.WriteLine(line.Substring(0, position));
WriteLineIndented(line.Substring(position + 1, line.Length - position - 1), leftMargin, width);
}
}
用法:
string s = "The path from the source file. The destination file will be automatically converted and copied in the same path with the source file.";
WriteLineIndented(s, 10, 80);
结果:
The path from the source file. The destination file will be automatically converted and copied in the same path with the source file.
您可以根据自己的情况轻松修改此方法-只需写下所有需要的内容,而不仅仅是在第一行中输入空格。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句