自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#格式化字符串學(xué)習(xí)總結(jié)

開發(fā) 后端
C#格式化字符串的學(xué)習(xí)中我們首先掌握的就是C#格式化字符串的定義,那么關(guān)于C#格式化字符串的應(yīng)用是如何的呢?本問就向你介紹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)制格式。

  1. double val=Math.PI;  
  2.  
  3. Console.WriteLine(val.ToString( )); //displays 3.14159265358979  
  4.  
  5. Console.WriteLine(val.ToString(”E”));//displays 3.141593E+000  
  6.  
  7. Console.WriteLine(val.ToString(”F3″);//displays 3.142  
  8.  
  9. int val=65535;  
  10.  
  11. Console.WriteLine(val.ToString(”x”)); //displays ffff  
  12.  
  13. Console.WriteLine(val.ToString(”X”)); //displays FFFF  
  14.  
  15. Single val=0.123F;  
  16.  
  17. Console.WriteLine(val.ToString(”p”)); //displays 12.30 %  
  18.  
  19. 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)。

  1. DateTime dt = DateTime.Now;  
  2. String date;  
  3. date = dt.ToString(“d“,DateTimeFormatInfo.InvariantInfo);  
  4.  
  5. Response.Write(date + “﹤/br﹥“);//07/22/2009  
  6.  
  7. date = dt.ToString(“G“, DateTimeFormatInfo.InvariantInfo);  
  8.  
  9. Response.Write(date + “﹤/br﹥“);//07/22/2009 14:54:37  
  10.  
  11. date = dt.ToString(“r“, DateTimeFormatInfo.InvariantInfo);  
  12. Response.Write(date + “﹤/br﹥“);//Wed, 22 Jul 2009 14:54:37 GMT  
  13.  
  14. date = dt.ToString(“T“, DateTimeFormatInfo.InvariantInfo);  
  15. Response.Write(date + “﹤/br﹥“);//14:54:37  
  16.  
  17. date = dt.ToString(“u“, DateTimeFormatInfo.InvariantInfo);  
  18. Response.Write(date + “﹤/br﹥“);//2009-07-22 14:54:37Z  
  19.  
  20. date = dt.ToString(“dd-MM-yyyy“, DateTimeFormatInfo.InvariantInfo);  
  21. Response.Write(date + “﹤/br﹥“);//22-07-2009  
  22.  

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 “左面”去空格

  1. String str = “Hello,Welcome to China!“;  
  2. String temp;  
  3. if (str.EndsWith(“!“))  
  4.  
  5. Response.Write(“str is endwith !“ + “﹤/br﹥“ );  
  6. //str is endwith !  
  7. int i= str.IndexOf(‘W‘);  
  8. Response.Write(“The first place of W is:“ + i + “﹤/br﹥“);  
  9. //The first place of W is:6  
  10.  
  11. temp = str.Insert(5, “ everyone“);  
  12. Response.Write(“temp Insert:“ + temp + “﹤/br﹥“);  
  13. //temp Insert:Hello everyone,Welcome to China!  
  14.  
  15. temp = str.Remove(5, 9);  
  16. Response.Write(“temp Remove:“ + temp + “﹤/br﹥“);  
  17. //temp Remove:Helloto China!  
  18.  
  19. Response.Write(“The length of str is:“ +   
  20. str.Length + “﹤/br﹥“);//The length of str is:23  
  21.  
  22. temp = str.Replace(‘!‘,‘$‘);  
  23. Response.Write(“temp Replace:“ + temp + “﹤/br﹥“);  
  24. //temp Replace:Hello,Welcome to China$  
  25.  
  26. temp = str.ToLower();  
  27. Response.Write(“temp ToLower:“ + temp + “﹤/br﹥“);  
  28. //temp ToLower:hello,welcome to china!  
  29.  
  30. temp = str.ToUpper();  
  31. Response.Write(“temp ToUpper:“ + temp + “﹤/br﹥“);  
  32. //temp ToUpper:HELLO,WELCOME TO CHINA!  
  33.  

C#格式化字符串的一些總結(jié)內(nèi)容就向你介紹到這里,那么希望通過實(shí)例的演示對(duì)你了解和學(xué)習(xí)C#格式化字符串有所幫助。

【編輯推薦】

  1. C#集合、C#動(dòng)態(tài)數(shù)組的概念淺析
  2. C#動(dòng)態(tài)數(shù)組的詳解介紹
  3. C#動(dòng)態(tài)數(shù)組的應(yīng)用詳解實(shí)例
  4. C#數(shù)組復(fù)制方法詳解
  5. C#判斷字符串應(yīng)用詳細(xì)解析
責(zé)任編輯:仲衡 來(lái)源: 互聯(lián)網(wǎng)
相關(guān)推薦

2009-09-03 18:45:06

GridView格式化

2010-02-01 16:46:07

C++格式化字符串

2024-02-22 09:46:04

C++字符串格式化開發(fā)

2021-06-09 07:55:18

Python格式化字符串

2009-08-24 17:06:37

C#字符串

2024-12-09 08:10:00

Python字符串格式化

2009-08-03 16:24:05

C#格式化

2020-06-28 08:26:41

Python開發(fā)工具

2017-01-16 16:33:06

Python 字符串漏洞

2022-05-09 14:04:27

Python字符串格式化輸出

2009-09-03 18:05:04

ASP.NET字符串格

2009-08-03 14:25:59

C#日期格式化

2009-09-04 12:22:41

C#日期格式化

2009-11-26 18:36:52

PHP函數(shù)sprint

2024-03-06 08:41:14

Python字符串格式化工具

2009-09-03 14:28:23

C#日期格式化

2009-09-04 13:19:59

C#代碼格式化

2009-09-03 14:20:21

C#日期格式化

2009-09-04 11:19:40

C#數(shù)字格式化

2009-07-30 16:23:07

C#日期格式化
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)