String.Format(string, arg0)中sring格式

复合格式字符串和对象列表将用作支持复合格式设置功能的方法的参数。复合格式字符串由零个或多个固定文本段与一个或多个格式项混和组成。固定文本是所选择的任何字符串,并且每个格式项对应于列表中的一个对象或装箱的结构。复合格式设置功能返回新的结果字符串,其中每个格式项都被列表中相应对象的字符串表示形式取代。

可考虑使用以下 Format 代码段。

string name = "Fred";

String.Format("Name = {0}, hours = {1:hh}", name, DateTime.Now);

固定文本为“Name = ”和“, hours = ”。格式项为“{0}”和“{1:hh}”,前者的索引为 0,对应于对象 myName,后者的索引为 1,对应于对象 DateTime.Now。

格式项语法

每个格式项都采用下面的形式并包含以下组件:

{index[,alignment][:formatString]}" xml:space="preserve" id="mt24">{ index[,alignment][:formatString]}

必须使用成对的大括号(“{”和“}”)。

索引组件:

强制“索引”组件(也叫参数说明符)是一个从 0 开始的数字,可标识对象列表中对应的项。也就是说,参数说明符为 0 的格式项列表中的第一个对象,参数说明符为 1 的格式项列表中的第二个对象,依次类推。下面的示例包括五个参数说明符,用于表示小于 10 的质数:

string primes;
primes = String.Format("Prime numbers less than 10: {0}, {1}, {2}, {3}, {4}",  1, 2, 3, 5, 7 );
Console.WriteLine(primes);
// The example displays the following output:
// Prime numbers less than 10: 1, 2, 3, 5, 7

通过指定相同的参数说明符,多个格式项可以引用对象列表中的同一个元素。例如,通过指定诸如“0x{0:X} {0:E} {0:N}”的复合格式字符串,可以将同一个数值设置为十六进制、科学记数法和数字格式,如下面的示例所示。

string multiple = String.Format("0x{0:X} {0:E} {0:N}", Int64.MaxValue);
Console.WriteLine(multiple);
// The example displays the following output:
//      0x7FFFFFFFFFFFFFFF 9.223372E+018 9,223,372,036,854,775,807.00
 

每个格式项都可以引用列表中的任一对象。例如,如果有三个对象,则可以通过指定类似于“{1} {0} {2}”的复合格式字符串来设置第二、第一和第三个对象的格式。格式项未引用的对象会被忽略。如果参数说明符指定了超出对象列表范围的项,将引发运行时 FormatException。

对齐组件:

可选的“对齐”组件是一个带符号的整数,指示首选的设置了格式的字段宽度。alignment is less than the length of the formatted string, alignment is ignored and the length of the formatted string is used as the field width." xml:space="preserve" id="mt40">如果“对齐”值小于设置了格式的字符串的长度,“对齐”会被忽略,并且使用设置了格式的字符串的长度作为字段宽度。alignment is positive and left-aligned if alignment is negative." xml:space="preserve" id="mt41">如果“对齐”为正数,字段中设置了格式的数据为右对齐;如果“对齐”为负数,字段中的设置了格式的数据为左对齐。如果需要填充,则使用空白。alignment is specified." xml:space="preserve" id="mt43">如果指定 alignment,就需要使用逗号。

下面的示例定义两个数组,一个包含雇员的姓名,另一个则包含雇员在两周内的工作小时数。复合格式字符串使 20 字符字段中的姓名左对齐,使 5 字符字段中的工作小时数右对齐。请注意“N1”标准格式字符串还用于设置带有小数位的小时数格式。

using System;
public class Example
{
   public static void Main()
   {
      string[] names = { "Adam", "Bridgette", "Carla", "Daniel",
                         "Ebenezer", "Francine", "George" };
      decimal[] hours = { 40, 6.667m, 40.39m, 82, 40.333m, 80,
                                 16.75m };
      Console.WriteLine("{0,-20} {1,5}
", "Name", "Hours");
      for (int ctr = 0; ctr < names.Length; ctr++)
         Console.WriteLine("{0,-20} {1,5:N1}", names[ctr], hours[ctr]);
 
   }
}
// The example displays the following output:
//       Name                 Hours
//
//       Adam                  40.0
//       Bridgette              6.7
//       Carla                 40.4
//       Daniel                82.0
//       Ebenezer              40.3
//       Francine              80.0
//       George                16.8

格式字符串组件

可选的“格式字符串”组件是适合正在设置格式的对象类型的格式字符串。DateTime object, or an enumeration format string if the corresponding object is an enumeration value." xml:space="preserve" id="mt50">如果相应的对象是数值,则指定标准或自定义的数字格式字符串;如果相应的对象是 DateTime 对象,则指定标准或自定义的日期和时间格式字符串;或者,如果相应的对象是枚举值,则指定枚举格式字符串。formatString is not specified, the general ("G") format specifier for a numeric, date and time, or enumeration type is used." xml:space="preserve" id="mt51">如果不指定 formatString,则对数字、日期和时间或者枚举类型使用常规(“G”)格式说明符。formatString is specified." xml:space="preserve" id="mt52">如果指定 formatString,则需要使用冒号。

下表列出了 .NET Framework 类库中支持预定义的格式字符串集的类型或类型的类别,并提供指向列出了支持的格式字符串的主题的链接。请注意,字符串格式化是一个可扩展的机制,可使用该机制定义所有现有类型的新的格式字符串,并定义受应用程序定义的类型支持的格式字符串集。IFormattable and ICustomFormatter interface topics." xml:space="preserve" id="mt55">有关详细信息,请参阅 IFormattable 和 ICustomFormatter 接口主题。

原文地址:https://www.cnblogs.com/yzl050819/p/5319122.html