如何:最好的办法画表中的控制台应用程序(C#) [英] How To: Best way to draw table in console app (C#)

查看:110
本文介绍了如何:最好的办法画表中的控制台应用程序(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的问题。
想象一下,我有很多数据在非常快的时间间隔改变。
我想显示的数据作为控制台应用程序表。 f.ex:

I have an interesting question. Imagine I have a lot of data changing in very fast intervals. I want to display that data as a table in console app. f.ex:

-------------------------------------------------------------------------
|    Column 1     |    Column 2     |    Column 3     |    Column 4     |
-------------------------------------------------------------------------
|                 |                 |                 |                 |
|                 |                 |                 |                 |
|                 |                 |                 |                 |
-------------------------------------------------------------------------

如何让事情变得快,以及如何解决列宽?我知道如何做到这一点在Java中,但我不它是如何在C#中完成的。

How to keep things fast and how to fix column widths ? I know how to do that in java, but I don't how it's done in C#.

推荐答案

您可以做类似如下:

static int tableWidth = 77;

static void PrintLine()
{
    Console.WriteLine(new string('-', tableWidth));
}

static void PrintRow(params string[] columns)
{
    int width = (tableWidth - columns.Length) / columns.Length;
    string row = "|";

    foreach (string column in columns)
    {
        row += AlignCentre(column, width) + "|";
    }

    Console.WriteLine(row);
}

static string AlignCentre(string text, int width)
{
    text = text.Length > width ? text.Substring(0, width - 3) + "..." : text;

    if (string.IsNullOrEmpty(text))
    {
        return new string(' ', width);
    }
    else
    {
        return text.PadRight(width - (width - text.Length) / 2).PadLeft(width);
    }
}

这篇关于如何:最好的办法画表中的控制台应用程序(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆