在控制台中打印对齐的文本

nikogram

我想在控制台的右侧打印一个合理的文本。就像所有控制台帮助命令一样。

例如,复制命令:通过键入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.");
安迪·科涅涅夫(Andy Korneyev)

好吧,作为“快速而肮脏”的方法来编写具有左缩进的字符串,您可以执行一些简单的计算,然后“逐个部分”地编写来控制台字符串。

样例代码:

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

(Java)在滞后控制台中逐个字母打印文本

来自分类Dev

在控制台中打印阵列

来自分类Dev

在控制台中打印时如何将打印出的形状从循环中移出以获取文本

来自分类Dev

在控制台中打印时如何将打印出的形状从循环中移出以获取文本

来自分类Dev

在python控制台中打印数据

来自分类Dev

未在控制台中打印功能返回

来自分类Dev

在控制台中打印/ etc / shadow文件

来自分类Dev

在Windows控制台中打印unicode

来自分类Dev

“ this.response”未在控制台中打印

来自分类Dev

控制台中文本的颜色异常

来自分类Dev

在控制台中打印,但不在页面上打印?

来自分类Dev

使用\ r和打印一些文本后,如何在控制台中清除一行?

来自分类Dev

在控制台中打印西班牙语文本并使用 Itext 7 将其写入 PDF 时出现问题

来自分类Dev

在控制台中打印一个实心方块

来自分类Dev

'\ b'在PyCharm控制台中不打印退格键

来自分类Dev

获取类似于在Chrome控制台中打印的功能名称

来自分类Dev

在交互式Scala控制台中打印整个结果

来自分类Dev

在Python中同时在控制台中打印2行

来自分类Dev

我如何设法使stderr消息不在控制台中打印?

来自分类Dev

Xcode 6,在LLDB控制台中打印定义的值?

来自分类Dev

删除“ 在JavaScript控制台中打印数组时

来自分类Dev

将代码链接打印到PyCharms控制台中

来自分类Dev

无法在控制台中打印我的json数据?

来自分类Dev

在控制台中打印阵列时长度错误

来自分类Dev

使用打印命令设置控制台中显示的行数

来自分类Dev

为什么在控制台中没有打印出括号?

来自分类Dev

在控制台中打印国际象棋棋盘的Python方式

来自分类Dev

我如何设法使stderr消息不在控制台中打印?

来自分类Dev

将代码链接打印到PyCharms控制台中

Related 相关文章

  1. 1

    (Java)在滞后控制台中逐个字母打印文本

  2. 2

    在控制台中打印阵列

  3. 3

    在控制台中打印时如何将打印出的形状从循环中移出以获取文本

  4. 4

    在控制台中打印时如何将打印出的形状从循环中移出以获取文本

  5. 5

    在python控制台中打印数据

  6. 6

    未在控制台中打印功能返回

  7. 7

    在控制台中打印/ etc / shadow文件

  8. 8

    在Windows控制台中打印unicode

  9. 9

    “ this.response”未在控制台中打印

  10. 10

    控制台中文本的颜色异常

  11. 11

    在控制台中打印,但不在页面上打印?

  12. 12

    使用\ r和打印一些文本后,如何在控制台中清除一行?

  13. 13

    在控制台中打印西班牙语文本并使用 Itext 7 将其写入 PDF 时出现问题

  14. 14

    在控制台中打印一个实心方块

  15. 15

    '\ b'在PyCharm控制台中不打印退格键

  16. 16

    获取类似于在Chrome控制台中打印的功能名称

  17. 17

    在交互式Scala控制台中打印整个结果

  18. 18

    在Python中同时在控制台中打印2行

  19. 19

    我如何设法使stderr消息不在控制台中打印?

  20. 20

    Xcode 6,在LLDB控制台中打印定义的值?

  21. 21

    删除“ 在JavaScript控制台中打印数组时

  22. 22

    将代码链接打印到PyCharms控制台中

  23. 23

    无法在控制台中打印我的json数据?

  24. 24

    在控制台中打印阵列时长度错误

  25. 25

    使用打印命令设置控制台中显示的行数

  26. 26

    为什么在控制台中没有打印出括号?

  27. 27

    在控制台中打印国际象棋棋盘的Python方式

  28. 28

    我如何设法使stderr消息不在控制台中打印?

  29. 29

    将代码链接打印到PyCharms控制台中

热门标签

归档