C#格式化字符串學(xué)習(xí)總結(jié)
C#格式化字符串的定義,C#格式化字符串就是按一定格式輸出的字符串,但是在類和結(jié)構(gòu)執(zhí)行ToString()方法后,都是為了顯示給定變量的內(nèi)容。但是,用戶常常希望以各種可能的方式顯示變量的內(nèi)容,在不同的文化或地區(qū)背景中有不同的格式。.NET基類System.DateTime就是最明顯的一個(gè)示例:可以把日期顯示為10 June 2008、10 Jun 2008、6/10/08 (美國(guó))、10/6/08 (英國(guó))或10.06.2008 (德國(guó))。
為了解決C#格式化字符串的問題總結(jié)了一下幾個(gè)方面,希望對(duì)你有所幫助。
C#格式化字符串之格式化數(shù)字
格式字符 說(shuō)明和關(guān)聯(lián)屬性
c、C 貨幣格式。
d、D 十進(jìn)制格式。
e、E 科學(xué)計(jì)數(shù)(指數(shù))格式。
f、F 固定點(diǎn)格式。
g、G 常規(guī)格式。
n、N 數(shù)字格式。
P、P 百分比
r、R 往返格式,確保將已轉(zhuǎn)換成字符串的數(shù)字轉(zhuǎn)換回?cái)?shù)字時(shí)具有與原數(shù)字相同的值。
x、X 十六進(jìn)制格式。
- double val=Math.PI;
- Console.WriteLine(val.ToString( )); //displays 3.14159265358979
- Console.WriteLine(val.ToString(”E”));//displays 3.141593E+000
- Console.WriteLine(val.ToString(”F3″);//displays 3.142
- int val=65535;
- Console.WriteLine(val.ToString(”x”)); //displays ffff
- Console.WriteLine(val.ToString(”X”)); //displays FFFF
- Single val=0.123F;
- Console.WriteLine(val.ToString(”p”)); //displays 12.30 %
- Console.WriteLine(val.ToString(”p1″)); //displays 12.3 %
C#格式化字符串之格式化日期
d 短日期模式
表示由當(dāng)前 ShortDatePattern 屬性定義的自定義 DateTime 格式字符串。
例如,用于固定區(qū)域性的自定義格式字符串為“MM/dd/yyyy”。
G 常規(guī)日期/時(shí)間模式(短時(shí)間)
表示短日期 (d) 和短時(shí)間 (t) 模式的組合,由空格分隔。
G 常規(guī)日期/時(shí)間模式(長(zhǎng)時(shí)間)
表示短日期 (d) 和長(zhǎng)時(shí)間 (T) 模式的組合,由空格分隔。
M 或 m 月日模式
表示由當(dāng)前 MonthDayPattern 屬性定義的自定義 DateTime 格式字符串。
例如,用于固定區(qū)域性的自定義格式字符串為“MMMM dd”。
R 或 r RFC1123 模式
表示由當(dāng)前 RFC1123Pattern 屬性定義的自定義 DateTime 格式字符串。該模式是定義的標(biāo)準(zhǔn),并且屬性是只讀的。因此,無(wú)論所使用的區(qū)域性或所提供的格式提供程序是什么,它總是相同的。
定義格式字符串為“ddd, dd MMM yyyy HH’:'mm’:’ss ‘GMT’”。
格式化不會(huì)修改正在格式化的 DateTime 對(duì)象的值。因此,應(yīng)用程序在使用此格式說(shuō)明符之前必須將該值轉(zhuǎn)換為協(xié)調(diào)世界時(shí) (UTC)。
T 長(zhǎng)時(shí)間模式
表示由當(dāng)前 LongTimePattern 屬性定義的自定義 DateTime 格式字符串。
例如,用于固定區(qū)域性的自定義格式字符串為“HH:mm:ss”。
U 通用的可排序日期/時(shí)間模式
表示由當(dāng)前 UniversalSortableDateTimePattern 屬性定義的自定義 DateTime 格式字符串。此模式是定義的標(biāo)準(zhǔn),并且屬性是只讀的。因此,無(wú)論所使用的區(qū)域性或所提供的格式提供程序是什么,它總是相同的。
自定義格式字符串為“yyyy’-'MM’-'dd HH’:'mm’:’ss’Z'”。
格式化日期和時(shí)間時(shí)不進(jìn)行時(shí)區(qū)轉(zhuǎn)換。因此,應(yīng)用程序在使用此格式說(shuō)明符之前必須將本地日期和時(shí)間轉(zhuǎn)換為協(xié)調(diào)世界時(shí) (UTC)。
- DateTime dt = DateTime.Now;
- String date;
- date = dt.ToString(“d“,DateTimeFormatInfo.InvariantInfo);
- Response.Write(date + “﹤/br﹥“);//07/22/2009
- date = dt.ToString(“G“, DateTimeFormatInfo.InvariantInfo);
- Response.Write(date + “﹤/br﹥“);//07/22/2009 14:54:37
- date = dt.ToString(“r“, DateTimeFormatInfo.InvariantInfo);
- Response.Write(date + “﹤/br﹥“);//Wed, 22 Jul 2009 14:54:37 GMT
- date = dt.ToString(“T“, DateTimeFormatInfo.InvariantInfo);
- Response.Write(date + “﹤/br﹥“);//14:54:37
- date = dt.ToString(“u“, DateTimeFormatInfo.InvariantInfo);
- Response.Write(date + “﹤/br﹥“);//2009-07-22 14:54:37Z
- date = dt.ToString(“dd-MM-yyyy“, DateTimeFormatInfo.InvariantInfo);
- Response.Write(date + “﹤/br﹥“);//22-07-2009
C#格式化字符串之其他函數(shù)
Endswith 末尾是否匹配指定string
Indexof 索引指向int start開始的***個(gè)string
Insert 插入string
Length 長(zhǎng)度,數(shù)組為大小
Remove 從string中刪除,數(shù)組為刪除一個(gè)string
Replace 替換
StartsWith 開始是否與指定string匹配
Tolower 小寫
Toupper 大寫
Trim 兩頭去除空格
TrimEnd “右面”去空格
TrimStart “左面”去空格
- String str = “Hello,Welcome to China!“;
- String temp;
- if (str.EndsWith(“!“))
- Response.Write(“str is endwith !“ + “﹤/br﹥“ );
- //str is endwith !
- int i= str.IndexOf(‘W‘);
- Response.Write(“The first place of W is:“ + i + “﹤/br﹥“);
- //The first place of W is:6
- temp = str.Insert(5, “ everyone“);
- Response.Write(“temp Insert:“ + temp + “﹤/br﹥“);
- //temp Insert:Hello everyone,Welcome to China!
- temp = str.Remove(5, 9);
- Response.Write(“temp Remove:“ + temp + “﹤/br﹥“);
- //temp Remove:Helloto China!
- Response.Write(“The length of str is:“ +
- str.Length + “﹤/br﹥“);//The length of str is:23
- temp = str.Replace(‘!‘,‘$‘);
- Response.Write(“temp Replace:“ + temp + “﹤/br﹥“);
- //temp Replace:Hello,Welcome to China$
- temp = str.ToLower();
- Response.Write(“temp ToLower:“ + temp + “﹤/br﹥“);
- //temp ToLower:hello,welcome to china!
- temp = str.ToUpper();
- Response.Write(“temp ToUpper:“ + temp + “﹤/br﹥“);
- //temp ToUpper:HELLO,WELCOME TO CHINA!
C#格式化字符串的一些總結(jié)內(nèi)容就向你介紹到這里,那么希望通過實(shí)例的演示對(duì)你了解和學(xué)習(xí)C#格式化字符串有所幫助。
【編輯推薦】