1、string.Format
Format(IFormatProvider, String, Object, Object, Object) 将字符串中的格式项替换为三个指定对象的字符串表示形式。 参数提供区域性特定的格式设置信息
格式化数值结果表
Format(String, Object) 将字符串中的一个或多个格式项替换为指定对象的字符串表示形式
1 DateTime birthdate = new DateTime(1993, 7, 28); 2 //{0}中的0表述格式项的索引,有几个格式项就有几个索引,数字从0开始。 3 string birth = string.Format("date is {0}", birthdate.ToLongDateString()); 4 Console.Write(birth); 5 Console.ReadLine(); 6 // 示例显示如下输出: 7 // date is 1993年7月28日
View Code
Format(String, Object[]) 将指定字符串中的格式项替换为指定数组中相应对象的字符串表示形式
1 DateTime date1 = new DateTime(2009, 7, 1); 2 TimeSpan hiTime = new TimeSpan(14, 17, 32); 3 TimeSpan loTime = new TimeSpan(3, 16, 10); 4 /** 5 * {0:d}中的d表示格式项转化为十进制 6 * {1,11}中的11第二个对象的字符串表示形式在11个字符的字段中右对齐。 7 * (如果第一个对象的字符串表示形式的长度超过11个字符, 则将忽略首选字段宽度, 8 * 并将整个字符串插入到结果字符串中。) 9 * */ 10 string result1 = String.Format("Temperature on {0:d}:n{1,11}: {2} degrees (hi)n", 11 date1, hiTime,loTime); 12 Console.WriteLine(result1); 13 Console.WriteLine(); 14 //若要在字段中左对齐字符串, 请在字段宽度前面加上负号, 如{0,-12}定义12个字符左对齐字段。 15 string result2 = String.Format("Temperature on {0:d}:n{1,-12}: {2} degrees (hi)n", 16 new object[] { date1, hiTime, loTime}); 17 Console.WriteLine(result2); 18 // 示例显示如下输出: 19 //Temperature on 2009 / 7 / 1: 20 //14:17:32: 03:16:10 degrees(hi) 21 //Temperature on 2009 / 7 / 1: 22 //14:17:32 : 03:16:10 degrees(hi) 23 Console.ReadLine();
View Code
Format(IFormatProvider, String, Object) 将指定字符串中的一个或多个格式项替换为对应对象的字符串表示形式。 参数提供区域性特定的格式设置信息
Format(IFormatProvider, String, Object[]) 将字符串中的格式项替换为指定数组中相应对象的字符串表示形式。 参数提供区域性特定的格式设置信息
string[] cultureNames = { "en-US", "fr-FR", "de-DE", "es-ES" }; DateTime dateToDisplay = new DateTime(2009, 9, 1, 18, 32, 0); double value = 9164.32; Console.WriteLine("Culture Date Value\n"); foreach (string cultureName in cultureNames) { System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName); string output = String.Format(culture, "{0,-11} {1,-35:D} {2:N}", culture.Name, dateToDisplay, value); Console.WriteLine(output); } // 示例显示如下输出: // Culture Date Value // // en-US Tuesday, September 01, 2009 9,164.32 // fr-FR mardi 1 septembre 2009 9 164,32 // de-DE Dienstag, 1. September 2009 9.164,32 // es-ES martes, 01 de septiembre de 2009 9.164,32
View Code
Format(String, Object, Object) 将字符串中的格式项替换为两个指定对象的字符串表示形式
1 Dictionary<DateTime, Double> temperatureInfo = new Dictionary<DateTime, Double>(); 2 temperatureInfo.Add(new DateTime(2010, 6, 1, 14, 0, 0), 87.46); 3 temperatureInfo.Add(new DateTime(2010, 12, 1, 10, 0, 0), 36.81); 4 5 Console.WriteLine("Temperature Information:\n"); 6 string output; 7 foreach (var item in temperatureInfo) 8 { 9 output = String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F", 10 item.Key, item.Value); 11 Console.WriteLine(output); 12 } 13 // The example displays output like the following: 14 // Temperature Information: 15 // 16 // Temperature at 2:00 PM on 6/1/2010: 87.5°F 17 // Temperature at 10:00 AM on 12/1/2010: 36.8°F
View Code
Format(IFormatProvider, String, Object, Object) 将字符串中的格式项替换为两个指定对象的字符串表示形式。 参数提供区域性特定的格式设置信息
Format(String, Object, Object, Object) 将字符串中的格式项替换为三个指定对象的字符串表示形式
1 string formatString = " {0,10} ({0,8:X8})\n" + 2 "And {1,10} ({1,8:X8})\n" + 3 " = {2,10} ({2,8:X8})"; 4 int value1 = 16932; 5 int value2 = 15421; 6 string result = String.Format(formatString, 7 value1, value2, value1 & value2); 8 Console.WriteLine(result); 9 // The example displays the following output: 10 // 16932 (00004224) 11 // And 15421 (00003C3D) 12 // = 36 (00000024)
View Code
原文地址:http://www.cnblogs.com/kezhang/p/16890501.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性