C#-如何使用选项卡正确缩进字符串数据? [英] C# - How to Properly Indent String Data with Tabs?
问题描述
在我的C#控制台程序中,我有4个变量。它们的名称和类型如下:
int ID
bool状态
bool可用
int Count
我希望能够将它们打印到控制台,缩进如下: / p>
但是,当我使用标签 \ t,用于格式化我的字符串,它不考虑文本宽度,并且所有值都按如下方式任意缩进:
如何解决此问题?我不想使用任何第三方库,而只是使用.NET功能,例如String.Format()。
您可以使用以下空格填充它们:
Console.WriteLine( {0,-10} \t {1, -5} \t {2,-5} \t {3,-10},ID,状态,可用,计数);
如果您想将它们右对齐,则:
Console.WriteLine( {0,10} \t {1,5} \t {2,5} \t {3,10}, ID,状态,可用,计数);
我将填充设置为以字符串形式表示的整数或布尔值的最大可能长度。您可能需要对其进行调整以适应您的列标题。
In my C# Console program I have 4 variables. Their names and types are as follows:
int ID
bool Status
bool Available
int Count
I would like to be able to print them to the Console, nicely indented as follows:
However, when I use the tabs "\t", to format my string, it does not take into account the text width, and all the values are indented waywardly as follows:
How do I fix this? I don't want to use any third party libraries, but simply .NET functionality such as String.Format().
You can pad them with spaces like this:
Console.WriteLine("{0,-10}\t{1,-5}\t{2,-5}\t{3,-10}", ID, Status, Available, Count);
And if you want to right-align them instead:
Console.WriteLine("{0,10}\t{1,5}\t{2,5}\t{3,10}", ID, Status, Available, Count);
I set the padding to the longest possible length of an integer or boolean represented in string form. You may have to adjust it to account for your column titles.
这篇关于C#-如何使用选项卡正确缩进字符串数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!