C#格式化字符串學習總結
C#格式化字符串的定義,C#格式化字符串就是按一定格式輸出的字符串,但是在類和結構執行ToString()方法后,都是為了顯示給定變量的內容。但是,用戶常常希望以各種可能的方式顯示變量的內容,在不同的文化或地區背景中有不同的格式。.NET基類System.DateTime就是最明顯的一個示例:可以把日期顯示為10 June 2008、10 Jun 2008、6/10/08 (美國)、10/6/08 (英國)或10.06.2008 (德國)。
為了解決C#格式化字符串的問題總結了一下幾個方面,希望對你有所幫助。
C#格式化字符串之格式化數字
格式字符 說明和關聯屬性
c、C 貨幣格式。
d、D 十進制格式。
e、E 科學計數(指數)格式。
f、F 固定點格式。
g、G 常規格式。
n、N 數字格式。
P、P 百分比
r、R 往返格式,確保將已轉換成字符串的數字轉換回數字時具有與原數字相同的值。
x、X 十六進制格式。
- 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 短日期模式
表示由當前 ShortDatePattern 屬性定義的自定義 DateTime 格式字符串。
例如,用于固定區域性的自定義格式字符串為“MM/dd/yyyy”。
G 常規日期/時間模式(短時間)
表示短日期 (d) 和短時間 (t) 模式的組合,由空格分隔。
G 常規日期/時間模式(長時間)
表示短日期 (d) 和長時間 (T) 模式的組合,由空格分隔。
M 或 m 月日模式
表示由當前 MonthDayPattern 屬性定義的自定義 DateTime 格式字符串。
例如,用于固定區域性的自定義格式字符串為“MMMM dd”。
R 或 r RFC1123 模式
表示由當前 RFC1123Pattern 屬性定義的自定義 DateTime 格式字符串。該模式是定義的標準,并且屬性是只讀的。因此,無論所使用的區域性或所提供的格式提供程序是什么,它總是相同的。
定義格式字符串為“ddd, dd MMM yyyy HH’:'mm’:’ss ‘GMT’”。
格式化不會修改正在格式化的 DateTime 對象的值。因此,應用程序在使用此格式說明符之前必須將該值轉換為協調世界時 (UTC)。
T 長時間模式
表示由當前 LongTimePattern 屬性定義的自定義 DateTime 格式字符串。
例如,用于固定區域性的自定義格式字符串為“HH:mm:ss”。
U 通用的可排序日期/時間模式
表示由當前 UniversalSortableDateTimePattern 屬性定義的自定義 DateTime 格式字符串。此模式是定義的標準,并且屬性是只讀的。因此,無論所使用的區域性或所提供的格式提供程序是什么,它總是相同的。
自定義格式字符串為“yyyy’-'MM’-'dd HH’:'mm’:’ss’Z'”。
格式化日期和時間時不進行時區轉換。因此,應用程序在使用此格式說明符之前必須將本地日期和時間轉換為協調世界時 (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#格式化字符串之其他函數
Endswith 末尾是否匹配指定string
Indexof 索引指向int start開始的***個string
Insert 插入string
Length 長度,數組為大小
Remove 從string中刪除,數組為刪除一個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#格式化字符串的一些總結內容就向你介紹到這里,那么希望通過實例的演示對你了解和學習C#格式化字符串有所幫助。
【編輯推薦】