国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

點滴拾遺-自定義Format控制String.Format行為

2019-11-14 14:12:40
字體:
來源:轉載
供稿:網友

點擊下載示例代碼

String.Format 一重載方法的簽名如下

1 public static string Format(2     IFormatPRovider provider,3     string format,4     params Object[] args5 )

可以通過自定義 IFormatProvider 接口來控制 String.Format 執行過程中的特定行為。

 

過程如下

1, 自定義類實現 IFormatProvider 接口(object GetFormat 方法)

1 public object GetFormat(Type formatType)2 {3     if (formatType == typeof(ICustomFormatter))4         return this;5     else6         return null;7 }

 

2, 實現 ICustomFormatter 借口(string Format 方法)

摘自 MSDN。功能為控制 Int64 type 的string輸出。

 1 public string Format(string fmt, object arg, IFormatProvider formatProvider) 2 { 3     // Provide default formatting if arg is not an Int64.  4     if (arg.GetType() != typeof(Int64)) 5         try 6         { 7             return HandleOtherFormats(fmt, arg); 8         } 9         catch (FormatException e)10         {11             throw new FormatException(String.Format("The format of '{0}' is invalid.", fmt), e);12         }13 14     // Provide default formatting for unsupported format strings. 15     string ufmt = fmt.ToUpper(CultureInfo.InvariantCulture);16     if (!(ufmt == "H" || ufmt == "I"))17         try18         {19             return HandleOtherFormats(fmt, arg);20         }21         catch (FormatException e)22         {23             throw new FormatException(String.Format("The format of '{0}' is invalid.", fmt), e);24         }25 26     // Convert argument to a string. 27     string result = arg.ToString();28 29     // If account number is less than 12 characters, pad with leading zeroes. 30     if (result.Length < ACCT_LENGTH)31         result = result.PadLeft(ACCT_LENGTH, '0');32     // If account number is more than 12 characters, truncate to 12 characters. 33     if (result.Length > ACCT_LENGTH)34         result = result.Substring(0, ACCT_LENGTH);35 36     if (ufmt == "I")                    // Integer-only format.  37         return result;38     // Add hyphens for H format specifier. 39     else                                          // Hyphenated format. 40         return result.Substring(0, 5) + "-" + result.Substring(5, 3) + "-" + result.Substring(8);41 }

 

使用實現好的 Format 類

 1 long acctNumber; 2 double balance; 3 DaysOfWeek wday; 4 string output; 5  6 acctNumber = 104254567890; 7 balance = 16.34; 8 wday = DaysOfWeek.Monday; 9 10 output = String.Format((new AcctNumberFormat(),11                        "On {2}, the balance of account {0:H} was {1:C2}.",12                        acctNumber, balance, wday);13 Console.WriteLine(output);

 

執行結果如下

 

參考資料

Culture invariant Decimal.TryParse()  http://stackoverflow.com/questions/23131414/culture-invariant-decimal-tryparse

What does IFormatProvider do?  http://stackoverflow.com/questions/506676/what-does-iformatprovider-do

IFormatProvider Interface  https://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沿河| 孙吴县| 巴南区| 岳普湖县| 棋牌| 新化县| 武隆县| 孟津县| 宁武县| 文成县| 澎湖县| 平遥县| 禹城市| 海口市| 合阳县| 安远县| 浦东新区| 会同县| 甘谷县| 峨山| 乌什县| 金阳县| 铜川市| 余江县| 旬邑县| 双鸭山市| 天水市| 屯昌县| 文化| 榆树市| 美姑县| 当雄县| 五家渠市| 游戏| 铜川市| 崇仁县| 广德县| 山东| 承德县| 北海市| 密云县|