C# 之 String.Format详解

https://blog.csdn.net/zhaocg00/article/details/124539625

因为Format方法接收的参数是Object类型,所以对于值类型会涉及到装箱的操作

下面通过一些例子来详细介绍string.Format的用法:

如果你想要往另外一个字符串中插入某个object、variable、或者expression的值,那么就可以尝试使用String.Format,例如下面的例子:

1、

Decimal pricePerOunce = 17.36m
String s = String.Format("The current price is {0} per ounce", pricePerOunce);
Console.WriteLine(s);
// Result: The current price is 17.36 per ounce.

2、
你也可以去控制 大括号里的值 的 格式:

Decimal pricePerOunce = 17.36m;
String s = String.Format("The current price is {0:C2} per ounce.", pricePerOunce);
Console.WriteLine(s);
// Result if current culture is en-US:
// The current price is $17.36 per ounce.

3、
除了控制格式之外,你还可以控制 对齐(alignment) 和 间隔 (spacing)

4、

String.Format的参数列表中,先是一个格式化的字符串,后面跟要往该字符串指定位置(即 { } 的位置)中插入的对象或者表达式(数量可以是任意多个,虽然String.Format提供了好几种重载方式,其实你并不用去关心究竟在用哪一个)。

大括号里可以写数字,对应了这些待插入项的序号(即第几个),例如:

string s = String.Format("At {0}, the temperature is {1}°C.", DateTime.Now, 20.4);
Console.WriteLine(s);
// Output similar to: 'At 4/10/2015 9:29:41 AM, the temperature is 20.4°C.'

5、
控制格式(Control Formating)

如上文所说,我们可以在大括号 { } 里通过添加数字来指定插入参数列表中的第几个数(例如 {0} ,表示格式化字符串后面的第一个参数),同时我们也可以在数字后面使用冒号‘:’再后缀一些符号来控制更具体一些的格式。

例如::d 提取了时间里的年月日 :t 提取了小时和分钟

string s = String.Format("It is now {0:d} at {0:t}", DateTime.Now);
Console.WriteLine(s);
// Output similar to: 'It is now 4/10/2015 at 10:04 AM'

6、
这里读者可能会疑问:我咋知道有哪些字符串格式呢?确实这些格式化的种类还是比较多的,光靠记忆肯定是不行的,这里给出官方链接,按需要去查即可:

Standard numeric format strings​​​​​​

https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

 

Standard date and time format strings | Microsoft Docs

https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings

 

Standard TimeSpan format strings | Microsoft Docs

https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-timespan-format-strings

 

Enumeration format strings | Microsoft Docs

https://learn.microsoft.com/en-us/dotnet/standard/base-types/enumeration-format-strings

 

你也可以使用一些格式的标识符(Format Specifier)来定制自己需要的格式类型

7、

控制间距(Control Spacing)

你可以定义插入到目标字符串的字符串片段的宽度,例如,可以使用 {0,12} 表示插入的字符串的宽度为12

可以用于文本对齐,例如:

int[] years = { 2013, 2014, 2015 };
int[] population = { 1025632, 1105967, 1148203 };
var sb = new System.Text.StringBuilder();
sb.Append(String.Format("{0,6} {1,15}\n\n", "Year", "Population"));
for (int index = 0; index < years.Length; index++)
sb.Append(String.Format("{0,6} {1,15:N0}\n", years[index], population[index]));

Console.WriteLine(sb);

// Result:
// Year Population
//
// 2013 1,025,632
// 2014 1,105,967
// 2015 1,148,203

8、
控制左右对齐 (Control Alignment)

默认是右对齐,可以使用 如 {0, -12} 来设置为左对齐:

int[] years = { 2013, 2014, 2015 };
int[] population = { 1025632, 1105967, 1148203 };
String s = String.Format("{0,-10} {1,-10}\n\n", "Year", "Population");
for(int index = 0; index < years.Length; index++)
s += String.Format("{0,-10} {1,-10:N0}\n",
years[index], population[index]);
Console.WriteLine($"\n{s}");
// Result:
// Year Population
//
// 2013 1,025,632
// 2014 1,105,967

————————————————

版权声明:本文为CSDN博主「KevinZhaocg」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhaocg00/article/details/124539625

posted @ 2022-10-26 17:14  yinghualeihenmei  阅读(365)  评论(0编辑  收藏  举报