3
string s = "cat".PadRight(8);
string s2 = "poodle".PadRight(8);

Console.Write(s);
Console.WriteLine("feline");
Console.Write(s2);
Console.WriteLine("canine");

Console.WriteLine();

for (int i = 0; i < 30; i += 8)
{
   Console.Write(string.Format("{0} record(s) found.".PadRight(30, '.'), i));
   Console.WriteLine("(DONE).");
}
Console.Read();

这给了我这个输出 在此处输入图像描述

为什么那些(DONE)没有正确对齐feline and canine

4

2 回答 2

4

用这个:

string.Format("{0} record(s) found.", i).PadRight(30, '.')

实际上.PadRight(30, '.')调用内部,string.Format所以字符串首先被填充然后格式化。

于 2013-07-06T11:28:45.440 回答
0
for (int i = 0; i < 30; i += 8)
            {
                if (i<10) Console.Write("0"+string.Format("{0} record(s) found.".PadRight(30, '.'), i));  
                else Console.Write(string.Format("{0} record(s) found.".PadRight(30, '.'), i));
                Console.WriteLine("(DONE).");
            }
于 2013-07-06T11:51:36.187 回答