//前面那個本來想重新編輯的,但是那個編輯器之前被我調到Markdown之后,改回Tiny MCE編輯器不出來
在System.Int32中定義了4個ToString方法

1.無參的ToString()是重寫Object的,(or ValueType)的2,3,4用法相同,第三個可以自定義Provider
int實現了IFormattable接口

IFormattable中只定義了一個ToString方法,format參數,和IFormatProvide類型的provider
在.NET中實現了IFormatProvider的類型,只有幾個,其中包括SystemGlobalzation.CultureInfoIFormatProvider中定義了一個GetFormat方法(type),用來獲取System.Globalization命名空間下的NumberFormatInfo和DateTimeFormatInfo實例,這兩個類,同CultureInfo一樣,也是IFormatProvider的實例,因為目前.NET中涉及國際化操作的,也就數字,和日期.NumberFormatInfo和DateTimeFormatInfo如果是獲取關于日期,數字要格式化所需的東西.從DateTimeFormatInfo節選如下,還包括 中文,日語等等相關的內容

DateTimeFormatInfo,NumberFormatInfo是IFormatProvider實例,他們的GetFormat方法,根據參數type,返回this,或者null
實例:


1 public static void Main() 2 { 3 int i = 999; 4 5 //ToString() 6 Console.WriteLine(i.ToString());//999 7 8 //ToString(format) 9 Console.WriteLine(i.ToString("x"));//hex 3e710 Console.WriteLine(i.ToString("X"));//Hex 3E711 Console.WriteLine(i.ToString("C"));//貨幣格式 ¥999.0012 13 //ToString(format,provider)14 //InvariantCulture無特定文化信息,999前面那符號是 國際通用貨幣符號 15 Console.WriteLine(i.ToString("C",System.Globalization.CultureInfo.InvariantCulture));//¤999.0016 //en-US 語言-國家信息17 Console.WriteLine(i.ToString("C",new System.Globalization.CultureInfo("en")));//$999.0018 19 System.Console.ReadKey();20 }View Code ToString(),ToString(format)實際都等同于調用ToString(format,provider)版本,第一個參數為空時,默認為null或者"G"(一般情況下)第二個慘參數為空,則根據System.Globalization.CultureInfo.CurrentCulture or System.Threading.Thread.CurrentThread.CurrentCulture來獲取當前調用線程的區域文化信息
String.Format(format,param object[] arg),例如String.Format("The Post with id equals {0:x4} is wiite on {1}",1,DateTime.Now)
Format方法在碰到{0}這樣的標記時,會根據冒號后面的作為format,來調用arg的ToString(format);同時Format的重載允許傳遞一個自定義Provider
功能:將char類型按unicode輸出,格式u:小寫的16進制格式U:大寫的16進制格式

結果顯示為
代碼為

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace CLR_String_Char 7 { 8 public class UnicodeFormatProvider : ICustomFormatter,IFormatProvider 9 {10 public object GetFormat(Type formatType)11 {12 if(formatType == typeof(ICustomFormatter))13 {14 return this;15 }16 else17 {18 return System.Globalization.CultureInfo.CurrentCulture.GetFormat(formatType);19 }20 }21 22 public string Format(string format,object arg,IFormatProvider formatProvider)23 {24 var str = string.Empty;25 if(arg.GetType() == typeof(char) &&26 format != null27 )//是char28 {29 if(format=="u")30 {31 str = "http://u" + ((int)((char)arg)).ToString("x4");32 }33 else if(format == "U")34 {35 str = "http://u" + ((int)((char)arg)).ToString("X4");36 }37 }38 else if(arg is IFormattable)//arg 實現了IFormattable接口39 {40 //formatprovider41 str = (arg as IFormattable).ToString(format,null);42 }43 else if(arg != null)44 {45 str = arg.ToString();46 }47 return str;48 }49 }50 }View Code 在上面代碼中,昨天寫的,今天想想漏掉一種情況,當arg是char類型,且format不為空,format又不等于'U'和'u'的情況下,返回String.Empty是錯誤的,應該交給常規情況來處理
重復代碼很多,可以抽象出一個基類CustomerFormatter出來
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace CLR_String_Char 7 { 8 public abstract class CustomFormatter<T> : ICustomFormatter,IFormatProvider 9 {10 //IFormatProvider.GetFormat11 public object GetFormat(Type formatType)12 {13 if(formatType == typeof(ICustomFormatter))14 {15 //要的就是CustomFormatter16 return this;17 }18 else19 {20 return System.Globalization.CultureInfo.CurrentCulture.GetFormat(formatType);21 }22 }23 24 //ICustomFormatter.Format25 public string Format(string format,object arg,IFormatProvider formatProvider)26 {27 if(arg.GetType() == typeof(T) &&28 format != null29 )//是char30 {31 return FormatCustom(format,(T)arg);32 }33 return FormatRegular(format,arg);34 }35 36 //處理常規字符37 public string FormatRegular(string format,object arg)38 {39 if(arg is IFormattable)//arg 實現了IFormattable接口40 {41 //formatprovider42 return (arg as IFormattable).ToString(format,null);43 }44 else if(arg != null)45 {46 return arg.ToString();47 }48 return string.Empty;49 }50 51 //處理 自定義格式 類型,如果不重寫,也返回FormatRegular52 public virtual string FormatCustom(string format,T arg)53 {54 return FormatRegular(format,arg);55 }56 }57 }再次實現UnicodeFormatter時就簡單多了,只需要繼承自CustomFormatter<需要處理的類型>,再重寫FormatCustom方法即可
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace CLR_String_Char 7 { 8 public class UnicodeFormatProvider : CustomFormatter<char> 9 {10 //處理所有arg is T,format不為空的情況11 public override string FormatCustom(string format,char arg)12 {13 if(format == "U")14 {15 return "http://u"+((int)arg).ToString("X4");16 }17 if(format == "u")18 {19 return "http://u"+((int)arg).ToString("x4");20 }21 22 //是char,format不為空,交給FormatRegular來處理,如果不支持,拋出FormatException23 return base.FormatCustom(format,arg);24 }25 }26 }練練手,簡單的DatetimeFormat,zh-cn格式輸出咱常用的yyyy-MM-dd HH:mm:ss形式
1 class MyDateTimeFormatter : CustomFormatter<DateTime> 2 { 3 public override string FormatCustom(string format,DateTime arg) 4 { 5 //自定義了一個格式:zh-cn 6 if(format.Equals(
新聞熱點
疑難解答