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<DateTime, Double> temperatureInfo = new Dictionary<DateTime, Double>(); 
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)将字符串中的格式项替换为三个指定对象的字符串表示形式。 参数提供区域性特定的格式设置信息。

常用的格式化数值结果表:
在这里插入图片描述

  • 6
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#中,string.Format是一个用于格式化字符串的方法。它可以将指定的参数插入到格式字符串中的占位符中。引用\[1\]和引用\[2\]提供了一些常见的示例,展示了如何使用string.Format来格式化数字。例如,可以使用"{0:N2}"来将一个数字格式化为带有两位小数的数字字符串。引用\[3\]列出了string.Format方法的不同重载形式,可以根据需要选择适合的重载形式来格式化字符串。 #### 引用[.reference_title] - *1* [C#string.format用法详解](https://blog.csdn.net/alili1991/article/details/101253536)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [C# string.Format](https://blog.csdn.net/weixin_53370274/article/details/119328962)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [C#String.Format详解](https://blog.csdn.net/zhaocg00/article/details/124539625)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值