跳转到主内容
思享编程网:思考分享,玩转编程世界!

C# string.Format()方法的使用

Format(String, Object)将字符串中的一个或多个格式项替换为指定对象的字符串表示形式。

DateTime birthdate = new DateTime(1993, 7, 28);

//{0}中的0表述格式项的索引,有几个格式项就有几个索引,数字从0开始。

string birth = string.Format("date is {0}", birthdate.ToLongDateString()); Console.Write(birth); Console.ReadLine(); // 示例显示如下输出: // date is 1993年7月28日

Format(String, Object[])将指定字符串中的格式项替换为指定数组中相应对象的字符串表示形式。

DateTime date1 = new DateTime(2009, 7, 1);

TimeSpan hiTime = new TimeSpan(14, 17, 32); TimeSpan loTime = new TimeSpan(3, 16, 10);

/** * {0:d}中的d表示格式项转化为十进制 * {1,11}中的11第二个对象的字符串表示形式在11个字符的字段中右对齐。

* (如果第一个对象的字符串表示形式的长度超过11个字符, 则将忽略首选字段宽度, * 并将整个字符串插入到结果字符串中。

) * */ string result1 = String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n", date1, hiTime,loTime); Console.WriteLine(result1); Console.WriteLine(); //若要在字段中左对齐字符串, 请在字段宽度前面加上负号, 如{0,-12}定义12个字符左对齐字段。

string result2 = String.Format("Temperature on {0:d}:\n{1,-12}: {2} degrees (hi)\n", new object[] { date1, hiTime, loTime}); Console.WriteLine(result2); // 示例显示如下输出: //Temperature on 2009 / 7 / 1: //14:17:32: 03:16:10 degrees(hi)

//Temperature on 2009 / 7 / 1: //14:17:32 : 03:16:10 degrees(hi) Console.ReadLine();

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

Format(String, Object, Object)将字符串中的格式项替换为两个指定对象的字符串表示形式。

Dictionary temperatureInfo = new Dictionary();

temperatureInfo.Add(new DateTime(2010, 6, 1, 14, 0, 0), 87.46); temperatureInfo.Add(new DateTime(2010, 12, 1, 10, 0, 0), 36.81);

Console.WriteLine("Temperature Information:\n"); string output; foreach (var item in temperatureInfo) { output = String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F", item.Key, item.Value); Console.WriteLine(output); } // The example displays output like the following: // Temperature Information: // // Temperature at 2:00 PM on 6/1/2010: 87.5°F // Temperature at 10:00 AM on 12/1/2010: 36.8°F

Format(IFormatProvider, String, Object, Object)将字符串中的格式项替换为两个指定对象的字符串表示形式。

参数提供区域性特定的格式设置信息。

Format(String, Object, Object, Object)将字符串中的格式项替换为三个指定对象的字符串表示形式。

string formatString = " {0,10} ({0,8:X8})\n" +

"And {1,10} ({1,8:X8})\n" + " = {2,10} ({2,8:X8})"; int value1 = 16932; int value2 = 15421; string result = String.Format(formatString, value1, value2, value1 & value2); Console.WriteLine(result); // The example displays the following output: // 16932 (00004224) // And 15421 (00003C3D) // = 36 (00000024)

Format(IFormatProvider, String, Object, Object, Object)将字符串中的格式项替换为三个指定对象的字符串表示形式。

参数提供区域性特定的格式设置信息。

常用的格式化数值结果表:

相关文章