Skip navigation.

在公路上爬行

jiang's blog

在C#中格式化字符串

String Formatting in C#(英文)

格式:String.Format("{index[,alignment][:formatString]}", objects);
说明:(摘录于MSDN
index
从零开始的整数,指示对象列表中要格式化的元素。如果由索引指定的对象是空引用(Visual Basic 中为 Nothing),则格式项将被空字符串 ("") 替换。

alignment
可选整数,指示包含格式化值的区域的最小宽度。如果格式化值的长度小于对齐,则用空格填充该区域。如果对齐为负,则格式化的值将在该区域中左对齐;如果为正,则格式化的值将右对齐。如果没有指定对齐,则该区域的长度为格式化值的长度。如果指定对齐,则需用逗号分隔。

alignment到是不怎么常用,例:
String.Format(”->{1,10}<-”, “Hello”); -> Hello<-
String.Format(”->{1,-10}<-”, “Hello”); ->Hello <-

formatString
格式化代码的可选字符串。如果没有指定 formatString,并且对应的参数实现了 IFormattable 接口,则将空引用 (Nothing) 用作 IFormattable.ToString 格式字符串。因此,IFormattable.ToString 的所有实现都必须允许空引用 (Nothing) 作为格式字符串,并以 String 的形式返回对象表示形式的默认格式设置。如果指定 formatString,则必须使用冒号。

常用的格式化方法请查看英文原文

:irked: 这里编辑器排版太恼火了。

使用 ADO.NET 的最佳实践Visual Studio 2005 Code Snippets

How to use Quote function:

  1. Select some text
  2. Click on the Quote link

Write a comment

Comment
(BBcode and HTML is turned off for anonymous user comments.)

If you can't read the words, press the small reload icon.


Smilies

December 2009
S M T W T F S
November 2009January 2010
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31