如果你熟悉Microsoft Foundation Classes(MFC)的CString,Windows Template Library(WTL)的CString或者Standard Template Library(STL)的字串類,那麼你對String.Format方法肯定很熟悉。在C#中也經常使用這個方法來格式化字串,比如下面這樣:
intx=16;
decimaly=3.57m;
stringh=String.Format("item{0}sellsat{1:C}",x,y);
Console.WriteLine(h);
在我的機器上,可以得到下面的輸出:
item16sellsat¥3.57
也許你的機器上的輸出和這個不太一樣。這是正常的,本文稍後就會解釋這個問題。
在我們日常使用中,更多的是使用Console.WriteLine方法來輸出一個字串。其實String.Format和Console.WriteLine有很多共同點。兩個方法都有很多重載的格式並且採用無固定參數的對象數組作為最後一個參數。下面的兩個語句會產生同樣的輸出。
Console.WriteLine("Hello{0}{1}{2}{3}{4}{5}{6}{7}{8}",123,45.67,true,'Q',4,5,6,7,'8');
stringu=String.Format("Hello{0}{1}{2}{3}{4}{5}{6}{7}{8}",123,45.67,true,'Q',4,5,6,7,'8');
Console.WriteLine(u);
輸出如下:
Hello12345.67TrueQ45678
Hello12345.67TrueQ45678
2 字串格式
String.Format和WriteLine都遵守同樣的格式化規則。格式化的格式如下:"{ N [, M ][: formatString ]}", arg1, ... argN,在這個格式中:
1) N是從0開始的整數,表示要格式化的參數的個數
2) M是一個可選的整數,表示格式化後的參數所佔的寬度,如果M是負數,那麼格式化後的值就是靠左對齊的,如果M是正數,那麼格式化後的值是靠右對齊的
3) formatString是另外一個可選的參數,表示格式代碼
argN表示要格式化的運算式,和N是對應的。
如果argN是空值,那麼就用一個Null 字元串來代替。如果沒有formatString,那麼就用參數N對應的ToString方法來格式化。下面的語句會產生同樣的輸出:
publicclassTestConsoleApp
{
publicstaticvoidMain(string[]args)
{
Console.WriteLine(123);
Console.WriteLine("{0}",123);
Console.WriteLine("{0:D3}",123);
}
}
輸出是:
123
123
123
也可以通過String.Format得到同樣的輸出。
strings=string.Format("123");
stringt=string.Format("{0}",123);
stringu=string.Format("{0:D3}",123);
Console.WriteLine(s);
Console.WriteLine(t);
Console.WriteLine(u);
因此有如下結論:
(,M)決定了格式化字串的寬度和對齊方向
(:formatString)決定了如何格式化資料,比如用貨幣符號,科學計數法或者16進位。就像下面這樣:
Console.WriteLine("{0,5}{1,5}",123,456); //靠右對齊
Console.WriteLine("{0,-5}{1,-5}",123,456); //靠左對齊
輸出是
123 456
123 456
也可以合并這些運算式,先放一個逗號,再放一個冒號。就像這樣:
Console.WriteLine("{0,-10:D6}{1,-10:D6}",123,456);
輸出是:
000123 000456
我們可以用這種格式化特性來對齊我們的輸出。
Console.WriteLine("n{0,-10}{1,-3}","Name","Salary");
Console.WriteLine("----------------");
Console.WriteLine("{0,-10}{1,6}","Bill",123456);
Console.WriteLine("{0,-10}{1,6}","Polly",7890);
輸出是:
Name Salary
----------------
Bill 123456
Polly 7890
3 格式化標識符
標準的數學格式字串用於返回通常使用的字串。它們通常象X0這樣的格式。X是格式化標識符,0是精度標識符。格式標識符號共有9種,它們代表了大多數常用的數字格式。就像下表所示:
| 字母 |
含義 |
| C或c |
Currency 貨幣格式 |
| D或d |
Decimal 十進位格式(十進位整數,不要和.Net的Decimal資料類型混淆了) |
| E或e |
Exponent 指數格式 |
| F或f |
Fixed point 固定精度格式 |
| G或g |
General 常用格式 |
| N或n |
用逗號分割千位的數字,比如1234將會被變成1,234 |
| P或p |
Percentage 百分符號格式 |
| R或r |
Round-trip 圓整(只用於浮點數)保證一個數字被轉化成字串以後可以再被轉回成同樣的數字 |
| X或x |
Hex 16進位格式 |
如果我們使用下面的表達方式,讓我們看看會發生什麼
publicclassFormatSpecApp
{
publicstaticvoidMain(string[]args)
{
inti=123456;
Console.WriteLine("{0:C}",i);//¥123,456.00
Console.WriteLine("{0:D}",i);//123456
Console.WriteLine("{0:E}",i);//1.234560E+005
Console.WriteLine("{0:F}",i);//123456.00
Console.WriteLine("{0:G}",i);//123456
Console.WriteLine("{0:N}",i);//123,456.00
Console.WriteLine("{0:P}",i);//12,345,600.00%
Console.WriteLine("{0:X}",i);//1E240
}
}
精度控制標識控制了有效數位個數或者十進位數小數的位元。
Console.WriteLine("{0:C5}",i);//¥123,456.00
Console.WriteLine("{0:D5}",i);//123456
Console.WriteLine("{0:E5}",i);//1.23456E+005
Console.WriteLine("{0:F5}",i);//123456.00000
Console.WriteLine("{0:G5}",i);//1.23456E5
Console.WriteLine("{0:N5}",i);//123,456.00000
Console.WriteLine("{0:P5}",i);//12,345,600.00000%
Console.WriteLine("{0:X5}",i);//1E240
R(圓整)格式僅僅對浮點數有效。這個值首先會用通用格式來格式化。對於雙精確度數有15位精度,對於單精確度數有7位精度。如果這個值可以被正確地解析回原始的數字,就會用通用格式符來格式化。如果不能解析回去的話,那麼就會用17位精度來格式化雙精確度數,用9位精度來格式化單精確度數。儘管我們可以在圓整標識符後面添加有效數位位元,但是它會被忽略掉。
doubled=1.2345678901234567890;
Console.WriteLine("Floating-Point:t{0:F16}",d); //1.2345678901234600
Console.WriteLine("Roundtrip:t{0:R16}",d); //1.2345678901234567
如果標準格式化標識符還不能滿足你。你可以使用圖形化格式字串來建立定製的字串輸出。圖形化格式化使用預留位置來表示最小位元,
最大位元,定位器號,負號的外觀以及其它數字記號的外觀。就像下表所示
| 符號 |
名稱 |
含義 |
| 0 |
0預留位置 |
用0填充不足的位元 |
| # |
數字預留位置 |
用#代替實際的位元 |
| . |
十進位小數點 |
|
| , |
千位分隔字元 |
用逗號進行千位分割,比如把1000分割成1,000 |
| % |
百分符號 |
顯示一個百分標識 |
| E+0 E-0 e+0 e-0 |
指數符號 |
用指數符號格式化輸出 |
| |
專一字元 |
用於傳統格式的格式化序列,比如"n"(新行) |
| 'ABC' "ABC" |
常量字串 |
顯示單引號或者雙引號裡面的字串 |
| ; |
地區分隔字元 |
如果數字會被格式化成整數,負數,或者0,用;來進行分隔 |
| ,. |
縮放符號 |
數字除以1000 |
看下面的例子:
doublei=123456.42;
Console.WriteLine();
Console.WriteLine("{0:000000.00}",i);//123456.42
Console.WriteLine("{0:00.00000000e+0}",i);//12.34564200e+4
Console.WriteLine("{0:0,.}",i); //123
Console.WriteLine("{0:#0.000}",i); //123456.420
Console.WriteLine("{0:#0.000;(#0.000)}",i); //123456.420
Console.WriteLine("{0:#0.000;(#0.000);<zero>}",i);//123456.420
Console.WriteLine("{0:#%}",i); //12345642%
i=-123456.42;
Console.WriteLine();
Console.WriteLine("{0:000000.00}",i);//-123456.42
Console.WriteLine("{0:00.00000000e+0}",i);//-12.34564200e+4
Console.WriteLine("{0:0,.}",i); //-123
Console.WriteLine("{0:#0.000}",i); //-123456.420
Console.WriteLine("{0:#0.000;(#0.000)}",i); //(123456.420)
Console.WriteLine("{0:#0;(#0);<zero>}",i);//(123456)
Console.WriteLine("{0:#%}",i); //-12345642%
i=0;
Console.WriteLine();
Console.WriteLine("{0:0,.}",i); //0
Console.WriteLine("{0:#0}",i); //0
Console.WriteLine("{0:#0;(#0)}",i); //0
Console.WriteLine("{0:#0;(#0);<zero>}",i);//<zero>
Console.WriteLine("{0:#%}",i); //%
4 數字字串的解析
所有的基礎類型都有ToString方法,它是從object類型中繼承過來的。所有的數實值型別都有Parse方法,它用字串為參數,並且返回相等的數值。比如
publicclassNumParsingApp
{
publicstaticvoidMain(string[]args)
{
inti=int.Parse("12345");
Console.WriteLine("i={0}",i);
intj=Int32.Parse("12345");
Console.WriteLine("j={0}",j);
doubled=Double.Parse("1.2345E+6");
Console.WriteLine("d={0:F}",d);
strings=i.ToString();
Console.WriteLine("s={0}",s);
}
}
輸出如下
i=12345
j=12345
d=1234500.00
s=12345
在預設狀況下,某些非數字字元是可以存在的。比如開頭和結尾的空白。逗號和小數點,加號和減號,因此,下面的Parse語句是一樣的
stringt=" -1,234,567.890 ";
//doubleg=double.Parse(t); //和下面的代碼幹同樣的事情
doubleg=double.Parse(t,
NumberStyles.AllowLeadingSign|
NumberStyles.AllowDecimalPoint|
NumberStyles.AllowThousands|
NumberStyles.AllowLeadingWhite|
NumberStyles.AllowTrailingWhite);
Console.WriteLine("g={0:F}",g);
輸出都是這樣
g=-1234567.89
注意到,如果你要使用NumberStyles,就要添加對System.Globalization的引用,然後就可以使用不同NumberStyles的組合或者其中的任意一種。如果你想相容貨幣符號,就需要使用重載的Parse方法,它們採用了NumberFormatInfo對象作為一個參數,然後你可以設定NumberFormatInfo的CurrencySymbol屬性來調用Parse方法,比如:
stringu="¥ -1,234,567.890 ";
NumberFormatInfoni=newNumberFormatInfo();
ni.CurrencySymbol="¥";
doubleh=Double.Parse(u,NumberStyles.Any,ni);
Console.WriteLine("h={0:F}",h);
上面的代碼有如下輸出
h=-1234567.89
除了NumberFormatInfo,還可以使用CultureInfo類。CultureInfo代表了某種特定的文化,包括文化的名字,書寫的方式,日曆的格式。對於某種特定文化的操作是非常普遍的情況,比如格式化日期和排序。文化的命名方式遵從RFC1766標準,使用<語言代碼2>-<省/地區碼2>的方式,其中的<語言代碼2>是兩個小寫字母,它們來自ISO639-1;<省/地區碼2>是兩個大寫字母,它們來自ISO3166。比如,美國英語是“en-US"。英國英語是"en-GB"。特立尼達和多巴哥英語是"en-TT"。例如,我們可以建立一個美國英語的CultureInfo對象並且基於這種文化將數字轉換成字串。
intk=12345;
CultureInfous=newCultureInfo("en-US");
stringv=k.ToString("c",us);
Console.WriteLine(v);
輸出是:
$12,345.00
要注意到,我們使用了重載的ToString方法,它把第一個格式化字串當成第一個參數,將一個CultureInfo對象(執行了IFormatProvider對象)作為第二個參數。這兒有第二個例子,對于丹麥人來說:
CultureInfodk=newCultureInfo("da-DK");
stringw=k.ToString("c",dk);
Console.WriteLine(w);
輸出是:
kr12.345,00
5 字串和日期
一個日期對象有個叫Ticks的屬性。它儲存了自從公元1年的1月1號上午12點開始的,以100納秒為間隔的時間。比如,Ticks值等於31241376000000000L表示公元100年,星期五,1月1號,上午12點這一時間。Ticks總是以100納秒為間隔遞增。
DateTime的值以儲存在DateTimeFormatInfo執行個體裡面的標準或者自訂的方式來表示。為了修改一個日期顯示的方式,DateTimeFormatInfo執行個體必須要是可寫的,以便我們寫入自訂的格式並且存入屬性中
usingSystem.Globalization;
publicclassDatesApp
{
publicstaticvoidMain(string[]args)
{
DateTimedt=DateTime.Now;
Console.WriteLine(dt);
Console.WriteLine("date={0},time={1}n",
dt.Date,dt.TimeOfDay);
}
}
代碼會產生下面的輸出
23/06/200117:55:10
date=23/06/200100:00:00,time=17:55:10.3839296
下表列出了標準的格式字串以及相關的DateTimeFormatInfo屬性
| D |
|
|
| D |
MM/dd/yyyy |
ShortDatePattern(短日期模式) |
| D |
dddd,MMMM dd,yyyy |
LongDatePattern(長日期模式) |
| F |
dddd,MMMM dd,yyyy HH:mm |
Full date and time (long date and short time)(全日期和時間模式) |
| F |
dddd,MMMM dd,yyyy HH:mm:ss |
FullDateTimePattern (long date and long time)(長日期和長時間) |
| G |
MM/dd/yyyy HH:mm |
General (short date and short time)(通用模式,短日期和短時間) |
| G |
MM/dd/yyyy HH:mm:ss |
General (short date and long time)(通用模式,短日期和長時間) |
| M,M |
MMMM dd |
MonthDayPattern(月天模式) |
| r,R |
ddd,dd MMM yyyy,HH':'mm':'ss 'GMT' |
RFC1123Pattern (RFC1123模式) |
| S |
yyyy-MM-dd HH:mm:ss |
SortableDateTimePattern (conforms to ISO 8601) using local time(使用本地時間的可排序模式) |
| T |
HH:mm |
ShortTimePattern (短時間模式) |
| T |
HH:mm:ss |
LongTimePattern(長時間模式) |
| U |
yyyy-MM-dd HH:mm:ss |
UniversalSortable-DateTimePattern (conforms to ISO 8601) using universal time(通用可排序模式) |
| U |
dddd,MMMM dd,yyyy,HH:mm:ss |
UniversalSortable-DateTimePattern(通用可排序模式) |
| y,Y |
MMMM,yyyy |
YearMonthPattern(年月模式) |
DateTimeFormatInfo.InvariantInfo屬性得到了預設的唯讀DateTimeFormatInfo執行個體,它與文化無關。你可以建立自訂的模式。要注意到的是InvariantInfo不一定和本地的格式一樣。Invariant等於美國格式。另外,如果你向DateTime.Format方法傳遞的第二個參數是null,DateTimeFormatInfo將會是預設的CurrentInfo。比如
Console.WriteLine(dt.ToString("d",dtfi));
Console.WriteLine(dt.ToString("d",null));
Console.WriteLine();
輸出是
06/23/2001
23/06/2001
對比選擇InvariantInfo和CurrentInfo的。
DateTimeFormatInfodtfi;
Console.Write("[I]nvariantor[C]urrentInfo?:");
if(Console.Read()=='I')
dtfi=DateTimeFormatInfo.InvariantInfo;
else
dtfi=DateTimeFormatInfo.CurrentInfo;
DateTimeFormatInfodtfi=DateTimeFormatInfo.InvariantInfo;
Console.WriteLine(dt.ToString("D",dtfi));
Console.WriteLine(dt.ToString("f",dtfi));
Console.WriteLine(dt.ToString("F",dtfi));
Console.WriteLine(dt.ToString("g",dtfi));
Console.WriteLine(dt.ToString("G",dtfi));
Console.WriteLine(dt.ToString("m",dtfi));
Console.WriteLine(dt.ToString("r",dtfi));
Console.WriteLine(dt.ToString("s",dtfi));
Console.WriteLine(dt.ToString("t",dtfi));
Console.WriteLine(dt.ToString("T",dtfi));
Console.WriteLine(dt.ToString("u",dtfi));
Console.WriteLine(dt.ToString("U",dtfi));
Console.WriteLine(dt.ToString("d",dtfi));
Console.WriteLine(dt.ToString("y",dtfi));
Console.WriteLine(dt.ToString("dd-MMM-yy",dtfi));
輸出是
[I]nvariantor[C]urrentInfo?:I
01/03/2002
03/01/2002
Thursday,03January2002
Thursday,03January200212:55
Thursday,03January200212:55:03
01/03/200212:55
01/03/200212:55:03
January03
Thu,03Jan200212:55:03GMT
2002-01-03T12:55:03
12:55
12:55:03
2002-01-0312:55:03Z
Thursday,03January200212:55:03
01/03/2002
2002January
03-Jan-02
[I]nvariantor[C]urrentInfo?:C
03/01/2002
03/01/2002
03January2002
03January200212:55
03January200212:55:47
03/01/200212:55
03/01/200212:55:47
03January
Thu,03Jan200212:55:47GMT
2002-01-03T12:55:47
12:55
12:55:47
2002-01-0312:55:47Z
03January200212:55:47
03/01/2002
January2002
03-Jan-02