Console.WriteLine输出字符格式化

引自:http://blog.csdn.net/xrongzhen/article/details/5477075

参考:http://msdn.microsoft.com/en-us/library/aakt1eab.aspx

其中格式项都采用如下形式:

{index[,alignment][:formatString]}   

 

其中"index"指索引占位符,这个肯定都知道;

",alignment"按字面意思显然是对齐方式,以","为标记;

":formatString"就是对输出格式的限定,以":"为标记。

 

alignment:可选,是一个带符号的整数,指示首选的格式化字段宽度。如果“对齐”值小于格式化字符串的长度,“对齐”会被忽略,并且使用格式化字符串的长度作为字段宽度。如果“对齐”为正数,字段的格式化数据为右对齐;如果“对齐”为负数,字段的格式化数据为左对齐。如果需要填充,则使用空白。如果指定“对齐”,就需要使用逗号。

 

示例:

 1 public static void Main()
 2         {
 3             SortedDictionary<string, string> sortedDictionary =
 4                new SortedDictionary<string, string>();
 5 
 6             int index = 0;
 7 
 8             sortedDictionary.Add(index++.ToString(), "object");
 9             sortedDictionary.Add(index++.ToString(), "byte");
10             sortedDictionary.Add(index++.ToString(), "uint");
11             sortedDictionary.Add(index++.ToString(), "ulong");
12             sortedDictionary.Add(index++.ToString(), "float");
13             sortedDictionary.Add(index++.ToString(), "char");
14             sortedDictionary.Add(index++.ToString(), "bool");
15             sortedDictionary.Add(index++.ToString(), "ushort");
16             sortedDictionary.Add(index++.ToString(), "decimal");
17             sortedDictionary.Add(index++.ToString(), "int");
18             sortedDictionary.Add(index++.ToString(), "sbyte");
19             sortedDictionary.Add(index++.ToString(), "short");
20             sortedDictionary.Add(index++.ToString(), "long");
21             sortedDictionary.Add(index++.ToString(), "void");
22             sortedDictionary.Add(index++.ToString(), "double");
23             sortedDictionary.Add(index++.ToString(), "string");
24 
25             Console.WriteLine("Key  Value    Hashcode");
26             Console.WriteLine("---  -------  ----------");
27             foreach (
28                 KeyValuePair<string, string> i in sortedDictionary)
29             {
30                 Console.WriteLine("{0,-5}{1,-9}{2}",
31                     i.Key, i.Value, i.Key.GetHashCode());
32             }
33         }

posted on 2013-08-15 15:34  pushaoxia  阅读(1612)  评论(0)    收藏  举报

导航