Ste*_*ith 76 c# console tabs alignment vertical-alignment
我有一种列显示,但最后两列似乎没有正确对齐.这是我目前的代码:
Console.WriteLine("Customer name "
+ "sales "
+ "fee to be paid "
+ "70% value "
+ "30% value");
for (int DisplayPos = 0; DisplayPos < LineNum; DisplayPos = DisplayPos + 1)
{
seventy_percent_value = ((fee_payable[DisplayPos] / 10.0) * 7);
thirty_percent_value = ((fee_payable[DisplayPos] / 10.0) * 3);
Console.WriteLine(customer[DisplayPos] + " "
+ sales_figures[DisplayPos] + " "
+ fee_payable[DisplayPos] + " "
+ seventy_percent_value + " "
+ thirty_percent_value);
}
Run Code Online (Sandbox Code Playgroud)
我是新手程序员,所以我可能不理解给出的所有建议,但如果您有任何建议,我将不胜感激!
小智 286
试试这个
Console.WriteLine("{0,10}{1,10}{2,10}{3,10}{4,10}",
customer[DisplayPos],
sales_figures[DisplayPos],
fee_payable[DisplayPos],
seventy_percent_value,
thirty_percent_value);
Run Code Online (Sandbox Code Playgroud)
大括号内的第一个数字是索引,第二个是对齐.第二个数字的符号表示字符串是左对齐还是右对齐.使用负数进行左对齐.
或者查看 http://msdn.microsoft.com/en-us/library/aa331875(v=vs.71).aspx
小智 49
只是为了添加roya的答案.在c#6.0中,您现在可以使用字符串插值:
Console.WriteLine($"{customer[DisplayPos],10}" +
$"{salesFigures[DisplayPos],10}" +
$"{feePayable[DisplayPos],10}" +
$"{seventyPercentValue,10}" +
$"{thirtyPercentValue,10}");
Run Code Online (Sandbox Code Playgroud)
这实际上可以是没有所有额外资金的一行,我只是认为这样可以更容易阅读.
您还可以在System.Console上使用静态导入,允许您执行以下操作:
using static System.Console;
WriteLine(/* write stuff */);
Run Code Online (Sandbox Code Playgroud)
Cod*_*ray 12
您应该将实际标签(\t
转义序列)嵌入到每个输出字符串中,而不是尝试将文本手动对齐到具有任意空格字符串的列中:
Console.WriteLine("Customer name" + "\t"
+ "sales" + "\t"
+ "fee to be paid" + "\t"
+ "70% value" + "\t"
+ "30% value");
for (int DisplayPos = 0; DisplayPos < LineNum; DisplayPos++)
{
seventy_percent_value = ((fee_payable[DisplayPos] / 10.0) * 7);
thirty_percent_value = ((fee_payable[DisplayPos] / 10.0) * 3);
Console.WriteLine(customer[DisplayPos] + "\t"
+ sales_figures[DisplayPos] + "\t"
+ fee_payable + "\t\t"
+ seventy_percent_value + "\t\t"
+ thirty_percent_value);
}
Run Code Online (Sandbox Code Playgroud)
我知道,非常老的线程,但是当有更长的字符串时,建议的解决方案并不是完全自动的.
因此,我创建了一个小辅助方法,它完全自动化.只需传入一个字符串数组列表,其中每个数组代表一行和数组中的每个元素,以及该行的元素.
该方法可以这样使用:
var lines = new List<string[]>();
lines.Add(new[] { "What", "Before", "After"});
lines.Add(new[] { "Name:", name1, name2});
lines.Add(new[] { "City:", city1, city2});
lines.Add(new[] { "Zip:", zip1, zip2});
lines.Add(new[] { "Street:", street1, street2});
var output = ConsoleUtility.PadElementsInLines(lines, 3);
Run Code Online (Sandbox Code Playgroud)
辅助方法如下:
public static class ConsoleUtility
{
/// <summary>
/// Converts a List of string arrays to a string where each element in each line is correctly padded.
/// Make sure that each array contains the same amount of elements!
/// - Example without:
/// Title Name Street
/// Mr. Roman Sesamstreet
/// Mrs. Claudia Abbey Road
/// - Example with:
/// Title Name Street
/// Mr. Roman Sesamstreet
/// Mrs. Claudia Abbey Road
/// <param name="lines">List lines, where each line is an array of elements for that line.</param>
/// <param name="padding">Additional padding between each element (default = 1)</param>
/// </summary>
public static string PadElementsInLines(List<string[]> lines, int padding = 1)
{
// Calculate maximum numbers for each element accross all lines
var numElements = lines[0].Length;
var maxValues = new int[numElements];
for (int i = 0; i < numElements; i++)
{
maxValues[i] = lines.Max(x => x[i].Length) + padding;
}
var sb = new StringBuilder();
// Build the output
bool isFirst = true;
foreach (var line in lines)
{
if (!isFirst)
{
sb.AppendLine();
}
isFirst = false;
for (int i = 0; i < line.Length; i++)
{
var value = line[i];
// Append the value with padding of the maximum length of any value for this element
sb.Append(value.PadRight(maxValues[i]));
}
}
return sb.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
希望这有助于某人.来源是我博客上的帖子:http://dev.flauschig.ch/wordpress/?p = 387
归档时间: |
|
查看次数: |
121067 次 |
最近记录: |