String.Format 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
根据指定的格式将对象值转换为字符串,并将其插入另一个字符串中。
如果不熟悉该方法 String.Format ,请参阅 String.Format 方法入门 ,获取快速概述。
重载
示例
在本文中,多个调用Format方法的示例分布在各处。 还可以下载一组 String.Format 完整的示例,其中包括 适用于 C# 的 .NET Core 项目。
以下是本文中包含的一些示例:
创建格式字符串
控制格式化输出
控件格式
控制间距
控件对齐方式
控制整型数字的数量
控制小数分隔符后的位数
在结果字符串中包含文本大括号
使格式字符串对文化敏感
使格式字符串具有文化敏感性
自定义格式化操作
开始使用 String.Format 方法
如果需要将对象、变量或表达式的值插入另一个字符串,请使用 String.Format 。 例如,可以将 Decimal 值 插入到字符串中,作为一个字符串显示给用户。
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.
let pricePerOunce = 17.36m
String.Format("The current price is {0} per ounce.", pricePerOunce)
|> printfn "%s"
// Result: The current price is 17.36 per ounce.
Dim pricePerOunce As Decimal = 17.36D
Dim s As String = String.Format("The current price is {0} per ounce.",
pricePerOunce)
' Result: The current price is 17.36 per ounce.
你可以控制该值的格式:
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.
let pricePerOunce = 17.36m
String.Format("The current price is {0:C2} per ounce.", pricePerOunce)
|> printfn "%s"
// Result if current culture is en-US:
// The current price is $17.36 per ounce.
Dim pricePerOunce As Decimal = 17.36D
Dim s As String = String.Format("The current price is {0:C2} per ounce.",
pricePerOunce)
' Result if current culture is en-US:
' The current price is $17.36 per ounce.
除了设置格式,还可以控制对齐和间距。
插入字符串
String.Format 以格式字符串开头,后跟一个或多个对象或表达式,这些对象或表达式将转换为字符串并在格式字符串中的指定位置插入。 例如:
decimal temp = 20.4m;
string s = String.Format("The temperature is {0}°C.", temp);
Console.WriteLine(s);
// Displays 'The temperature is 20.4°C.'
let temp = 20.4m
String.Format("The temperature is {0}°C.", temp)
|> printfn "%s"
// Displays 'The temperature is 20.4°C.'
Dim temp As Decimal = 20.4D
Dim s As String = String.Format("The temperature is {0}°C.", temp)
Console.WriteLine(s)
' Displays 'The temperature is 20.4°C.'
{0} 在格式字符串中是一个格式项。
0 是对象索引,其字符串值将插入到该位置。 (索引从 0 开始。如果要插入的对象不是字符串,则调用其 ToString 方法将其转换为一个,然后再将其插入到结果字符串中。
下面是另一个示例,该示例使用对象列表中的两个格式项和两个对象:
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.'
String.Format("At {0}, the temperature is {1}°C.", DateTime.Now, 20.4)
|> printfn "%s"
// Output similar to: 'At 4/10/2015 9:29:41 AM, the temperature is 20.4°C.'
Dim s As String = String.Format("At {0}, the temperature is {1}°C.",
Date.Now, 20.4)
' Output similar to: 'At 4/10/2015 9:29:41 AM, the temperature is 20.4°C.'
只要每个格式项的索引在对象列表中具有匹配的对象,就可以有任意数量的格式项和任意数量的对象。 你也不必担心调用哪个重载;编译器会为你选择合适的重载。
控件格式
可以使用格式字符串跟踪格式项中的索引,以控制对象的格式设置方式。 例如, {0:d} 将“d”格式字符串应用于对象列表中的第一个对象。 下面是一个包含单个对象和两个格式项的示例:
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'
String.Format("It is now {0:d} at {0:t}", DateTime.Now)
|> printfn "%s"
// Output similar to: 'It is now 4/10/2015 at 10:04 AM'
Dim s As String = String.Format("It is now {0:d} at {0:t}",
Date.Now)
' Output similar to: 'It is now 4/10/2015 at 10:04 AM'
许多类型支持格式字符串,包括所有数值类型( 标准 格式字符串和 自定义 格式字符串)、所有日期和时间( 标准 格式字符串和 自定义 格式字符串)以及时间间隔( 标准 字符串和 自定义 格式字符串)、所有枚举 类型枚举类型和GUID。 还可以向自己的类型添加对格式字符串的支持。
控制间距
您可以通过使用像 `{0,12}` 这样的语法来定义插入到结果字符串中的字符串的宽度,该语法会插入一个 12 字符的字符串。 在这种情况下,第一个对象的字符串表示形式在 12 个字符的字段中右对齐。 (如果第一个对象的字符串表示形式长度超过 12 个字符,则忽略首选字段宽度,并将整个字符串插入到结果字符串中。
下面的示例定义了一个 6 个字符的字段,用于保存字符串“Year”和某些年份字符串,以及用于保存字符串“Population”和某些总体数据的 15 个字符字段。 请注意,字符在字段中右对齐。
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
open System
open System.Text
let years = [| 2013; 2014; 2015 |]
let population = [| 1025632; 1105967; 1148203 |]
let sb = StringBuilder()
sb.Append(String.Format("{0,6} {1,15}\n\n", "Year", "Population")) |> ignore
for i = 0 to years.Length - 1 do
sb.Append(String.Format("{0,6} {1,15:N0}\n", years[i], population[i])) |> ignore
printfn $"{sb}"
// Result:
// Year Population
//
// 2013 1,025,632
// 2014 1,105,967
// 2015 1,148,203
Dim years() As Integer = {2013, 2014, 2015}
Dim population() As Integer = {1025632, 1105967, 1148203}
Dim sb As New StringBuilder()
sb.Append(String.Format("{0,6} {1,15}{2}{2}",
"Year", "Population", vbCrLf))
For index As Integer = 0 To years.Length - 1
sb.AppendFormat("{0,6} {1,15:N0}{2}",
years(index), population(index), vbCrLf)
Next
' Result:
' Year Population
'
' 2013 1,025,632
' 2014 1,105,967
' 2015 1,148,203
控制对齐
默认情况下,如果指定字段宽度,字符串在其字段中右对齐。 若要在字段中左对齐字符串,请为字段宽度加上负号,例如 {0,-12} 定义 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
// 2015 1,148,203
let years = [| 2013; 2014; 2015 |]
let population = [| 1025632; 1105967; 1148203 |]
let mutable s = String.Format("{0,-10} {1,-10}\n\n", "Year", "Population")
for i = 0 to years.Length - 1 do
s <- s + String.Format("{0,-10} {1,-10:N0}\n", years[i], population[i])
printfn $"\n{s}"
// Result:
// Year Population
//
// 2013 1,025,632
// 2014 1,105,967
// 2015 1,148,203
Dim years() As Integer = {2013, 2014, 2015}
Dim population() As Integer = {1025632, 1105967, 1148203}
Dim s As String = String.Format("{0,-10} {1,-10}{2}{2}",
"Year", "Population", vbCrLf)
For index As Integer = 0 To years.Length - 1
s += String.Format("{0,-10} {1,-10:N0}{2}",
years(index), population(index), vbCrLf)
Next
' Result:
' Year Population
'
' 2013 1,025,632
' 2014 1,105,967
' 2015 1,148,203
String.Format 利用复合格式设置功能。 如需了解更多信息,请参阅 Composite Formatting。
我该调用哪个方法?
| Objective | 调用的方法 |
|---|---|
| 使用当前文化的约定格式化一个或多个对象。 | 除包含 provider 参数的重载外,其余 Format 重载都包含一个 String 参数,后接一个或多个对象参数。 因此,不必确定要调用哪个 Format 重载。 语言编译器根据参数列表从没有 provider 参数的重载中选择适当的重载。 例如,如果参数列表有五个参数,编译器将调用该方法 Format(String, Object[]) 。 |
| 使用特定文化的约定格式化一个或多个对象。 | 每个以Format参数开头的provider重载后面跟一个String参数和一个或多个对象参数。 因此,不必确定要调用哪个特定 Format 重载。 语言编译器根据参数列表从具有 provider 参数的重载中选择适当的重载。 例如,如果参数列表有五个参数,编译器将调用该方法 Format(IFormatProvider, String, Object[]) 。 |
| 使用 ICustomFormatter 实现或 IFormattable 实现执行自定义格式设置操作。 | 具有 provider 参数的四个重载中的任何一个。 编译器根据参数列表从具有 provider 参数的重载中选择适当的重载。 |
简要介绍 'Format' 方法
Format 方法的每个重载都使用复合格式化功能,在复合格式字符串中包含基于零的索引占位符(称为格式项)。 在运行时,每个格式项都替换为参数列表中相应参数的字符串表示形式。 如果参数 null的值为,则格式项将 String.Empty替换为 。 例如,下面对Format(String, Object, Object, Object)方法的调用包括一个格式字符串,其中包含三个格式项:{0}、{1}和{2},以及一个包含三个项的参数列表。
DateTime dat = new DateTime(2012, 1, 17, 9, 30, 0);
string city = "Chicago";
int temp = -16;
string output = String.Format("At {0} in {1}, the temperature was {2} degrees.",
dat, city, temp);
Console.WriteLine(output);
// The example displays output like the following:
// At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.
open System
let dat = DateTime(2012, 1, 17, 9, 30, 0)
let city = "Chicago"
let temp = -16
String.Format("At {0} in {1}, the temperature was {2} degrees.", dat, city, temp)
|> printfn "%s"
// The example displays output like the following:
// At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.
Dim dat As Date = #1/17/2012 9:30AM#
Dim city As String = "Chicago"
Dim temp As Integer = -16
Dim output As String = String.Format("At {0} in {1}, the temperature was {2} degrees.",
dat, city, temp)
Console.WriteLine(output)
' The example displays the following output:
' At 1/17/2012 9:30:00 AM in Chicago, the temperature was -16 degrees.
格式项
格式项具有以下语法:
{index[,width][:formatString]}
括号表示可选元素。 需要使用左右大括号。 (要在格式字符串中包含字面上的左右大括号,请参阅复合格式化一文中的转义大括号部分。)
例如,设置货币值格式的格式项可能如下所示:
var value = String.Format("{0,-10:C}", 126347.89m);
Console.WriteLine(value);
open System
String.Format("{0,-10:C}", 126347.89m)
|> printfn "%s"
String.Format("{0,-10:C}", 126347.89D)
格式项具有以下元素:
index
参数的从零开始的索引,其字符串表示应包含在字符串的此位置。 如果此参数为null,那么在字符串的这个位置将包含一个空字符串。
width
Optional. 一个带符号整数,指示插入自变量的字段的总长度,以及它是右对齐(正整数)还是左对齐(负整数)。 如果省略 width,则相应的参数的字符串表示形式将插入到没有前导或尾随空格的字段中。
如果值 width 小于要插入的参数的长度, width 则忽略该参数的字符串表示形式的长度作为字段宽度。
formatString
Optional. 一个字符串,指定相应参数的结果字符串的格式。 如果省略 formatString,将调用相应的参数的无 ToString 参数方法以生成其字符串表示形式。 如果指定 formatString,格式项引用的参数必须实现 IFormattable 接口。 支持格式字符串的类型包括:
所有整型和浮点类型。 (请参阅 标准数字格式字符串 和 自定义数字格式字符串。
DateTime 和 DateTimeOffset。 (请参阅 标准日期和时间格式字符串 以及 自定义日期和时间格式字符串。
所有枚举类型。 (请参阅 枚举格式字符串。)
TimeSpan 值。 (请参阅 标准 TimeSpan 格式字符串 和 自定义 TimeSpan 格式字符串。)
GUID。 (请参阅方法 Guid.ToString(String) 。
但是,任何自定义类型都可以实现 IFormattable 或扩展现有类型的 IFormattable 实现。
以下示例使用 width 和 formatString 参数生成格式化输出。
// Create array of 5-tuples with population data for three U.S. cities, 1940-1950.
Tuple<string, DateTime, int, DateTime, int>[] cities =
{ Tuple.Create("Los Angeles", new DateTime(1940, 1, 1), 1504277,
new DateTime(1950, 1, 1), 1970358),
Tuple.Create("New York", new DateTime(1940, 1, 1), 7454995,
new DateTime(1950, 1, 1), 7891957),
Tuple.Create("Chicago", new DateTime(1940, 1, 1), 3396808,
new DateTime(1950, 1, 1), 3620962),
Tuple.Create("Detroit", new DateTime(1940, 1, 1), 1623452,
new DateTime(1950, 1, 1), 1849568) };
// Display header
var header = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n",
"City", "Year", "Population", "Change (%)");
Console.WriteLine(header);
foreach (var city in cities) {
var output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
(city.Item5 - city.Item3)/ (double)city.Item3);
Console.WriteLine(output);
}
// The example displays the following output:
// City Year Population Year Population Change (%)
//
// Los Angeles 1940 1,504,277 1950 1,970,358 31.0 %
// New York 1940 7,454,995 1950 7,891,957 5.9 %
// Chicago 1940 3,396,808 1950 3,620,962 6.6 %
// Detroit 1940 1,623,452 1950 1,849,568 13.9 %
// Create a list of 5-tuples with population data for three U.S. cities, 1940-1950.
let cities =
[ "Los Angeles", DateTime(1940, 1, 1), 1504277, DateTime(1950, 1, 1), 1970358
"New York", DateTime(1940, 1, 1), 7454995, DateTime(1950, 1, 1), 7891957
"Chicago", DateTime(1940, 1, 1), 3396808, DateTime(1950, 1, 1), 3620962
"Detroit", DateTime(1940, 1, 1), 1623452, DateTime(1950, 1, 1), 1849568 ]
// Display header
String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}\n", "City", "Year", "Population", "Change (%)")
|> printfn "%s"
for name, year1, pop1, year2, pop2 in cities do
String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
name, year1, pop1, year2, pop2,
double (pop2 - pop1) / double pop1)
|> printfn "%s"
// The example displays the following output:
// City Year Population Year Population Change (%)
//
// Los Angeles 1940 1,504,277 1950 1,970,358 31.0 %
// New York 1940 7,454,995 1950 7,891,957 5.9 %
// Chicago 1940 3,396,808 1950 3,620,962 6.6 %
// Detroit 1940 1,623,452 1950 1,849,568 13.9 %
Module Example4
Public Sub Main()
' Create array of 5-tuples with population data for three U.S. cities, 1940-1950.
Dim cities() =
{Tuple.Create("Los Angeles", #1/1/1940#, 1504277, #1/1/1950#, 1970358),
Tuple.Create("New York", #1/1/1940#, 7454995, #1/1/1950#, 7891957),
Tuple.Create("Chicago", #1/1/1940#, 3396808, #1/1/1950#, 3620962),
Tuple.Create("Detroit", #1/1/1940#, 1623452, #1/1/1950#, 1849568)}
' Display header
Dim header As String = String.Format("{0,-12}{1,8}{2,12}{1,8}{2,12}{3,14}",
"City", "Year", "Population", "Change (%)")
Console.WriteLine(header)
Console.WriteLine()
For Each city In cities
Dim output = String.Format("{0,-12}{1,8:yyyy}{2,12:N0}{3,8:yyyy}{4,12:N0}{5,14:P1}",
city.Item1, city.Item2, city.Item3, city.Item4, city.Item5,
(city.Item5 - city.Item3) / city.Item3)
Console.WriteLine(output)
Next
End Sub
End Module
' The example displays the following output:
' City Year Population Year Population Change (%)
'
' Los Angeles 1940 1,504,277 1950 1,970,358 31.0 %
' New York 1940 7,454,995 1950 7,891,957 5.9 %
' Chicago 1940 3,396,808 1950 3,620,962 6.6 %
' Detroit 1940 1,623,452 1950 1,849,568 13.9 %
如何设置参数的格式
格式项从字符串的开头按顺序处理。 每个格式项都有一个索引,该索引对应于方法的参数列表中的对象。 该方法 Format 检索参数并派生其字符串表示形式,如下所示:
如果参数为
null,该方法将插入 String.Empty 结果字符串。 你不必担心处理 NullReferenceException null 参数。如果调用 Format(IFormatProvider, String, Object[]) 重载并且
provider对象的 IFormatProvider.GetFormat 实现返回非 null ICustomFormatter 实现,则参数将传递给其 ICustomFormatter.Format(String, Object, IFormatProvider) 方法。 如果格式项包含参数,则会将其作为第一个formatString参数传递给该方法。 ICustomFormatter如果实现可用并生成非 null 字符串,则将该字符串作为参数的字符串表示形式返回;否则,下一步将执行。如果参数实现 IFormattable 接口,则调用其 IFormattable.ToString 实现。
参数的无参数
ToString方法会被调用,该方法会替代或继承自基类的实现。
有关截获对 ICustomFormatter.Format 方法的调用,并查看 Format 方法向复合格式字符串中每个格式项的格式化方法传递的信息的示例,请参阅 示例:拦截提供程序和罗马数字格式化程序。
有关详细信息,请参阅 处理订单。
具有相同索引的格式项
如果索引项的索引大于或等于参数列表中的参数数量,则 Format 方法将引发 FormatException 异常。 但是,format 可以包含比参数更多的格式项,只要多个格式项使用相同的索引。 在下面示例中调用方法Format(String, Object)时,参数列表中只有一个参数,但格式字符串中包含两个格式项:一个用于显示数字的十进制值,另一个用于显示其十六进制值。
short[] values= { Int16.MinValue, -27, 0, 1042, Int16.MaxValue };
Console.WriteLine("{0,10} {1,10}\n", "Decimal", "Hex");
foreach (short value in values)
{
string formatString = String.Format("{0,10:G}: {0,10:X}", value);
Console.WriteLine(formatString);
}
// The example displays the following output:
// Decimal Hex
//
// -32768: 8000
// -27: FFE5
// 0: 0
// 1042: 412
// 32767: 7FFF
open System
let values= [| Int16.MinValue; -27s; 0s; 1042s; Int16.MaxValue |]
printfn "%10s %10s\n" "Decimal" "Hex"
for value in values do
String.Format("{0,10:G}: {0,10:X}", value)
|> printfn "%s"
// The example displays the following output:
// Decimal Hex
//
// -32768: 8000
// -27: FFE5
// 0: 0
// 1042: 412
// 32767: 7FFF
Module Example1
Public Sub Main()
Dim values() As Short = {Int16.MinValue, -27, 0, 1042, Int16.MaxValue}
Console.WriteLine("{0,10} {1,10}", "Decimal", "Hex")
Console.WriteLine()
For Each value As Short In values
Dim formatString As String = String.Format("{0,10:G}: {0,10:X}", value)
Console.WriteLine(formatString)
Next
End Sub
End Module
' The example displays the following output:
' Decimal Hex
'
' -32768: 8000
' -27: FFE5
' 0: 0
' 1042: 412
' 32767: 7FFF
格式和文化
通常,参数列表中的对象通过使用当前区域性的约定(由 CultureInfo.CurrentCulture 属性返回)转换为其字符串表示形式。 可以通过调用 Format 的某个重载来控制这种行为,该重载包含一个 provider 参数。
provider 参数是一个 IFormatProvider 实现,它提供自定义和区域性特定的格式设置信息,用于审查格式设置过程。
该 IFormatProvider 接口具有单个成员, GetFormat该成员负责返回提供格式信息的对象。 .NET 有三个 IFormatProvider 实现,它们提供文化特定的格式化。
- CultureInfo。 其 GetFormat 方法返回一个区域性特定的 NumberFormatInfo 对象,用于设置数值的格式设置,并返回一个区域性特定的 DateTimeFormatInfo 对象,用于设置日期和时间值的格式。
- DateTimeFormatInfo,用于日期和时间值的文化特定格式化。 其 GetFormat 方法返回自身。
- NumberFormatInfo,用于特定于区域性的数值格式设置。 其 GetFormat(Type) 方法返回自身。
自定义格式设置操作
还可以调用 Format 方法的任何重载,这些重载都有一个类型为 provider 的 IFormatProvider 参数,用于执行自定义格式化操作。 例如,可以将整数的格式格式化为标识号或电话号码。 若要执行自定义格式设置,参数 provider 必须同时实现 IFormatProvider 接口和 ICustomFormatter 接口。 当Format方法接收到一个ICustomFormatter实现为provider参数时,该Format方法会调用其IFormatProvider.GetFormat实现并请求一个ICustomFormatter类型的对象。 然后,它调用返回 ICustomFormatter 的对象 Format 的方法来设置传递给它的复合字符串中的每个格式项的格式。
有关提供自定义格式解决方案的详细信息,请参阅 如何:定义和使用自定义数字格式提供程序 和 ICustomFormatter。 有关将整数转换为格式化自定义数字的示例,请参阅 示例:自定义格式设置作。 有关将无符号字节转换为罗马数字的示例,请参阅 示例:拦截提供程序和罗马数字格式化程序。
示例:自定义格式设置操作
本示例定义了一个格式提供程序,将整数值格式化为客户账户号码,形式为 x-xxxxx-xx。
using System;
public class TestFormatter
{
public static void Main()
{
int acctNumber = 79203159;
Console.WriteLine(String.Format(new CustomerFormatter(), "{0}", acctNumber));
Console.WriteLine(String.Format(new CustomerFormatter(), "{0:G}", acctNumber));
Console.WriteLine(String.Format(new CustomerFormatter(), "{0:S}", acctNumber));
Console.WriteLine(String.Format(new CustomerFormatter(), "{0:P}", acctNumber));
try {
Console.WriteLine(String.Format(new CustomerFormatter(), "{0:X}", acctNumber));
}
catch (FormatException e) {
Console.WriteLine(e.Message);
}
}
}
public class CustomerFormatter : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
public string Format(string format,
object arg,
IFormatProvider formatProvider)
{
if (! this.Equals(formatProvider))
{
return null;
}
else
{
if (String.IsNullOrEmpty(format))
format = "G";
string customerString = arg.ToString();
if (customerString.Length < 8)
customerString = customerString.PadLeft(8, '0');
format = format.ToUpper();
switch (format)
{
case "G":
return customerString.Substring(0, 1) + "-" +
customerString.Substring(1, 5) + "-" +
customerString.Substring(6);
case "S":
return customerString.Substring(0, 1) + "/" +
customerString.Substring(1, 5) + "/" +
customerString.Substring(6);
case "P":
return customerString.Substring(0, 1) + "." +
customerString.Substring(1, 5) + "." +
customerString.Substring(6);
default:
throw new FormatException(
String.Format("The '{0}' format specifier is not supported.", format));
}
}
}
}
// The example displays the following output:
// 7-92031-59
// 7-92031-59
// 7/92031/59
// 7.92031.59
// The 'X' format specifier is not supported.
open System
type CustomerFormatter() =
interface IFormatProvider with
member this.GetFormat(formatType) =
if formatType = typeof<ICustomFormatter> then
this
else
null
interface ICustomFormatter with
member this.Format(format, arg, formatProvider: IFormatProvider) =
if this.Equals formatProvider |> not then
null
else
let format =
if String.IsNullOrEmpty format then "G"
else format.ToUpper()
let customerString =
let s = string arg
if s.Length < 8 then
s.PadLeft(8, '0')
else s
match format with
| "G" ->
customerString.Substring(0, 1) + "-" +
customerString.Substring(1, 5) + "-" +
customerString.Substring 6
| "S" ->
customerString.Substring(0, 1) + "/" +
customerString.Substring(1, 5) + "/" +
customerString.Substring 6
| "P" ->
customerString.Substring(0, 1) + "." +
customerString.Substring(1, 5) + "." +
customerString.Substring 6
| _ ->
raise (FormatException $"The '{format}' format specifier is not supported.")
let acctNumber = 79203159
String.Format(CustomerFormatter(), "{0}", acctNumber)
|> printfn "%s"
String.Format(CustomerFormatter(), "{0:G}", acctNumber)
|> printfn "%s"
String.Format(CustomerFormatter(), "{0:S}", acctNumber)
|> printfn "%s"
String.Format(CustomerFormatter(), "{0:P}", acctNumber)
|> printfn "%s"
try
String.Format(CustomerFormatter(), "{0:X}", acctNumber)
|> printfn "%s"
with :? FormatException as e ->
printfn $"{e.Message}"
// The example displays the following output:
// 7-92031-59
// 7-92031-59
// 7/92031/59
// 7.92031.59
// The 'X' format specifier is not supported.
Module TestFormatter
Public Sub Main()
Dim acctNumber As Integer = 79203159
Console.WriteLine(String.Format(New CustomerFormatter, "{0}", acctNumber))
Console.WriteLine(String.Format(New CustomerFormatter, "{0:G}", acctNumber))
Console.WriteLine(String.Format(New CustomerFormatter, "{0:S}", acctNumber))
Console.WriteLine(String.Format(New CustomerFormatter, "{0:P}", acctNumber))
Try
Console.WriteLine(String.Format(New CustomerFormatter, "{0:X}", acctNumber))
Catch e As FormatException
Console.WriteLine(e.Message)
End Try
End Sub
End Module
Public Class CustomerFormatter : Implements IFormatProvider, ICustomFormatter
Public Function GetFormat(type As Type) As Object _
Implements IFormatProvider.GetFormat
If type Is GetType(ICustomFormatter) Then
Return Me
Else
Return Nothing
End If
End Function
Public Function Format(fmt As String, _
arg As Object, _
formatProvider As IFormatProvider) As String _
Implements ICustomFormatter.Format
If Not Me.Equals(formatProvider) Then
Return Nothing
Else
If String.IsNullOrEmpty(fmt) Then fmt = "G"
Dim customerString As String = arg.ToString()
if customerString.Length < 8 Then _
customerString = customerString.PadLeft(8, "0"c)
Select Case fmt
Case "G"
Return customerString.Substring(0, 1) & "-" & _
customerString.Substring(1, 5) & "-" & _
customerString.Substring(6)
Case "S"
Return customerString.Substring(0, 1) & "/" & _
customerString.Substring(1, 5) & "/" & _
customerString.Substring(6)
Case "P"
Return customerString.Substring(0, 1) & "." & _
customerString.Substring(1, 5) & "." & _
customerString.Substring(6)
Case Else
Throw New FormatException( _
String.Format("The '{0}' format specifier is not supported.", fmt))
End Select
End If
End Function
End Class
' The example displays the following output:
' 7-92031-59
' 7-92031-59
' 7/92031/59
' 7.92031.59
' The 'X' format specifier is not supported.
示例:拦截提供程序和罗马数字格式化程序
此示例定义一个自定义格式提供程序,该提供程序实现 ICustomFormatter 和 IFormatProvider 接口以执行两项作:
- 它显示传递给其 ICustomFormatter.Format 实现的参数。 这样,我们便可以查看该方法传递给尝试设置格式的每个对象的自定义格式实现的参数 Format(IFormatProvider, String, Object[]) 。 在调试应用程序时,这非常有用。
- 如果要格式化的对象是使用“R”标准格式字符串格式化的无符号字节值,则自定义格式化程序将数值格式化为罗马数字。
using System;
using System.Globalization;
public class InterceptProvider : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
public string Format(String format, Object obj, IFormatProvider provider)
{
// Display information about method call.
string formatString = format ?? "<null>";
Console.WriteLine("Provider: {0}, Object: {1}, Format String: {2}",
provider.GetType().Name, obj ?? "<null>", formatString);
if (obj == null) return String.Empty;
// If this is a byte and the "R" format string, format it with Roman numerals.
if (obj is Byte && formatString.ToUpper().Equals("R")) {
Byte value = (Byte) obj;
int remainder;
int result;
String returnString = String.Empty;
// Get the hundreds digit(s)
result = Math.DivRem(value, 100, out remainder);
if (result > 0)
returnString = new String('C', result);
value = (Byte) remainder;
// Get the 50s digit
result = Math.DivRem(value, 50, out remainder);
if (result == 1)
returnString += "L";
value = (Byte) remainder;
// Get the tens digit.
result = Math.DivRem(value, 10, out remainder);
if (result > 0)
returnString += new String('X', result);
value = (Byte) remainder;
// Get the fives digit.
result = Math.DivRem(value, 5, out remainder);
if (result > 0)
returnString += "V";
value = (Byte) remainder;
// Add the ones digit.
if (remainder > 0)
returnString += new String('I', remainder);
// Check whether we have too many X characters.
int pos = returnString.IndexOf("XXXX");
if (pos >= 0) {
int xPos = returnString.IndexOf("L");
if (xPos >= 0 & xPos == pos - 1)
returnString = returnString.Replace("LXXXX", "XC");
else
returnString = returnString.Replace("XXXX", "XL");
}
// Check whether we have too many I characters
pos = returnString.IndexOf("IIII");
if (pos >= 0)
if (returnString.IndexOf("V") >= 0)
returnString = returnString.Replace("VIIII", "IX");
else
returnString = returnString.Replace("IIII", "IV");
return returnString;
}
// Use default for all other formatting.
if (obj is IFormattable)
return ((IFormattable) obj).ToString(format, CultureInfo.CurrentCulture);
else
return obj.ToString();
}
}
public class FormatExample12
{
public static void Main()
{
int n = 10;
double value = 16.935;
DateTime day = DateTime.Now;
InterceptProvider provider = new InterceptProvider();
Console.WriteLine(String.Format(provider, "{0:N0}: {1:C2} on {2:d}\n", n, value, day));
Console.WriteLine(String.Format(provider, "{0}: {1:F}\n", "Today: ",
(DayOfWeek) DateTime.Now.DayOfWeek));
Console.WriteLine(String.Format(provider, "{0:X}, {1}, {2}\n",
(Byte) 2, (Byte) 12, (Byte) 199));
Console.WriteLine(String.Format(provider, "{0:R}, {1:R}, {2:R}\n",
(Byte) 2, (Byte) 12, (Byte) 199));
}
}
// The example displays the following output:
// Provider: InterceptProvider, Object: 10, Format String: N0
// Provider: InterceptProvider, Object: 16.935, Format String: C2
// Provider: InterceptProvider, Object: 1/31/2013 6:10:28 PM, Format String: d
// 10: $16.94 on 1/31/2013
//
// Provider: InterceptProvider, Object: Today: , Format String: <null>
// Provider: InterceptProvider, Object: Thursday, Format String: F
// Today: : Thursday
//
// Provider: InterceptProvider, Object: 2, Format String: X
// Provider: InterceptProvider, Object: 12, Format String: <null>
// Provider: InterceptProvider, Object: 199, Format String: <null>
// 2, 12, 199
//
// Provider: InterceptProvider, Object: 2, Format String: R
// Provider: InterceptProvider, Object: 12, Format String: R
// Provider: InterceptProvider, Object: 199, Format String: R
// II, XII, CXCIX
open System
open System.Globalization
type InterceptProvider() =
interface IFormatProvider with
member this.GetFormat(formatType) =
if formatType = typeof<ICustomFormatter> then
this
else
null
interface ICustomFormatter with
member _.Format(format, obj, provider: IFormatProvider) =
// Display information about method call.
let formatString =
if format = null then "<null>" else format
printfn $"Provider: {provider.GetType().Name}, Object: %A{obj}, Format String: %s{formatString}"
if obj = null then
String.Empty
else
// If this is a byte and the "R" format string, format it with Roman numerals.
match obj with
| :? byte as value when formatString.ToUpper().Equals "R" ->
let mutable returnString = String.Empty
// Get the hundreds digit(s)
let struct (result, remainder) = Math.DivRem(value, 100uy)
if result > 0uy then
returnString <- String('C', int result)
let value = byte remainder
// Get the 50s digit
let struct (result, remainder) = Math.DivRem(value, 50uy)
if result = 1uy then
returnString <- returnString + "L"
let value = byte remainder
// Get the tens digit.
let struct (result, remainder) = Math.DivRem(value, 10uy)
if result > 0uy then
returnString <- returnString + String('X', int result)
let value = byte remainder
// Get the fives digit.
let struct (result, remainder) = Math.DivRem(value, 5uy)
if result > 0uy then
returnString <- returnString + "V"
let value = byte remainder
// Add the ones digit.
if remainder > 0uy then
returnString <- returnString + String('I', int remainder)
// Check whether we have too many X characters.
let pos = returnString.IndexOf "XXXX"
if pos >= 0 then
let xPos = returnString.IndexOf "L"
returnString <-
if xPos >= 0 && xPos = pos - 1 then
returnString.Replace("LXXXX", "XC")
else
returnString.Replace("XXXX", "XL")
// Check whether we have too many I characters
let pos = returnString.IndexOf "IIII"
if pos >= 0 then
returnString <-
if returnString.IndexOf "V" >= 0 then
returnString.Replace("VIIII", "IX")
else
returnString.Replace("IIII", "IV")
returnString
// Use default for all other formatting.
| :? IFormattable as x ->
x.ToString(format, CultureInfo.CurrentCulture)
| _ ->
string obj
let n = 10
let value = 16.935
let day = DateTime.Now
let provider = InterceptProvider()
String.Format(provider, "{0:N0}: {1:C2} on {2:d}\n", n, value, day)
|> printfn "%s"
String.Format(provider, "{0}: {1:F}\n", "Today: ", DateTime.Now.DayOfWeek)
|> printfn "%s"
String.Format(provider, "{0:X}, {1}, {2}\n", 2uy, 12uy, 199uy)
|> printfn "%s"
String.Format(provider, "{0:R}, {1:R}, {2:R}\n", 2uy, 12uy, 199uy)
|> printfn "%s"
// The example displays the following output:
// Provider: InterceptProvider, Object: 10, Format String: N0
// Provider: InterceptProvider, Object: 16.935, Format String: C2
// Provider: InterceptProvider, Object: 1/31/2013 6:10:28 PM, Format String: d
// 10: $16.94 on 1/31/2013
//
// Provider: InterceptProvider, Object: Today: , Format String: <null>
// Provider: InterceptProvider, Object: Thursday, Format String: F
// Today: : Thursday
//
// Provider: InterceptProvider, Object: 2, Format String: X
// Provider: InterceptProvider, Object: 12, Format String: <null>
// Provider: InterceptProvider, Object: 199, Format String: <null>
// 2, 12, 199
//
// Provider: InterceptProvider, Object: 2, Format String: R
// Provider: InterceptProvider, Object: 12, Format String: R
// Provider: InterceptProvider, Object: 199, Format String: R
// II, XII, CXCIX
Imports System.Globalization
Public Class InterceptProvider : Implements IFormatProvider, ICustomFormatter
Public Function GetFormat(formatType As Type) As Object _
Implements IFormatProvider.GetFormat
If formatType Is GetType(ICustomFormatter) Then
Return Me
Else
Return Nothing
End If
End Function
Public Function Format(fmt As String, obj As Object, provider As IFormatProvider) As String _
Implements ICustomFormatter.Format
Dim formatString As String = If(fmt IsNot Nothing, fmt, "<null>")
Console.WriteLine("Provider: {0}, Object: {1}, Format String: {2}",
provider, If(obj IsNot Nothing, obj, "<null>"), formatString)
If obj Is Nothing Then Return String.Empty
' If this is a byte and the "R" format string, format it with Roman numerals.
If TypeOf(obj) Is Byte AndAlso formatString.ToUpper.Equals("R") Then
Dim value As Byte = CByte(obj)
Dim remainder As Integer
Dim result As Integer
Dim returnString As String = String.Empty
' Get the hundreds digit(s)
result = Math.DivRem(value, 100, remainder)
If result > 0 Then returnString = New String("C"c, result)
value = CByte(remainder)
' Get the 50s digit
result = Math.DivRem(value, 50, remainder)
If result = 1 Then returnString += "L"
value = CByte(remainder)
' Get the tens digit.
result = Math.DivRem(value, 10, remainder)
If result > 0 Then returnString += New String("X"c, result)
value = CByte(remainder)
' Get the fives digit.
result = Math.DivRem(value, 5, remainder)
If result > 0 Then returnString += "V"
value = CByte(remainder)
' Add the ones digit.
If remainder > 0 Then returnString += New String("I"c, remainder)
' Check whether we have too many X characters.
Dim pos As Integer = returnString.IndexOf("XXXX")
If pos >= 0 Then
Dim xPos As Integer = returnString.IndexOf("L")
If xPos >= 0 And xPos = pos - 1 Then
returnString = returnString.Replace("LXXXX", "XC")
Else
returnString = returnString.Replace("XXXX", "XL")
End If
End If
' Check whether we have too many I characters
pos = returnString.IndexOf("IIII")
If pos >= 0 Then
If returnString.IndexOf("V") >= 0 Then
returnString = returnString.Replace("VIIII", "IX")
Else
returnString = returnString.Replace("IIII", "IV")
End If
End If
Return returnString
End If
' Use default for all other formatting.
If TypeOf obj Is IFormattable Then
Return CType(obj, IFormattable).ToString(fmt, CultureInfo.CurrentCulture)
Else
Return obj.ToString()
End If
End Function
End Class
Module Example
Public Sub Main()
Dim n As Integer = 10
Dim value As Double = 16.935
Dim day As DateTime = Date.Now
Dim provider As New InterceptProvider()
Console.WriteLine(String.Format(provider, "{0:N0}: {1:C2} on {2:d}", n, value, day))
Console.WriteLine()
Console.WriteLine(String.Format(provider, "{0}: {1:F}", "Today",
CType(Date.Now.DayOfWeek, DayOfWeek)))
Console.WriteLine()
Console.WriteLine(String.Format(provider, "{0:X}, {1}, {2}\n",
CByte(2), CByte(12), CByte(199)))
Console.WriteLine()
Console.WriteLine(String.Format(provider, "{0:R}, {1:R}, {2:R}",
CByte(2), CByte(12), CByte(199)))
End Sub
End Module
' The example displays the following output:
' Provider: InterceptProvider, Object: 10, Format String: N0
' Provider: InterceptProvider, Object: 16.935, Format String: C2
' Provider: InterceptProvider, Object: 1/31/2013 6:10:28 PM, Format String: d
' 10: $16.94 on 1/31/2013
'
' Provider: InterceptProvider, Object: Today: , Format String: <null>
' Provider: InterceptProvider, Object: Thursday, Format String: F
' Today: : Thursday
'
' Provider: InterceptProvider, Object: 2, Format String: X
' Provider: InterceptProvider, Object: 12, Format String: <null>
' Provider: InterceptProvider, Object: 199, Format String: <null>
' 2, 12, 199
'
' Provider: InterceptProvider, Object: 2, Format String: R
' Provider: InterceptProvider, Object: 12, Format String: R
' Provider: InterceptProvider, Object: 199, Format String: R
' II, XII, CXCIX
FAQ
为什么建议使用字符串内插,而不是调用 String.Format 方法?
字符串内插为:
- 更灵活。 它可以在任何字符串中使用,而无需调用支持复合格式的方法。 否则,必须调用 Format 支持复合格式的方法或其他方法,例如 Console.WriteLine 或 StringBuilder.AppendFormat。
- 更易于阅读。 由于要插入字符串的表达式出现在内插表达式而不是参数列表中,因此内插字符串更易于编码和读取。 内插字符串还可用于字符串串联作,以生成更简洁、更清晰的代码。
以下两个代码示例的比较说明了内插字符串相对于字符串串联的优越性,以及对复合格式设置方法的调用。 以下示例中使用多个字符串串联作将生成详细且难以读取的代码。
string[] names = { "Balto", "Vanya", "Dakota", "Samuel", "Koani", "Yiska", "Yuma" };
string output = names[0] + ", " + names[1] + ", " + names[2] + ", " +
names[3] + ", " + names[4] + ", " + names[5] + ", " +
names[6];
output += "\n";
var date = DateTime.Now;
output += String.Format("It is {0:t} on {0:d}. The day of the week is {1}.",
date, date.DayOfWeek);
Console.WriteLine(output);
// The example displays the following output:
// Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
// It is 10:29 AM on 1/8/2018. The day of the week is Monday.
open System
let names = [| "Balto"; "Vanya"; "Dakota"; "Samuel"; "Koani"; "Yiska"; "Yuma" |]
let output =
names[0] + ", " + names[1] + ", " + names[2] + ", " +
names[3] + ", " + names[4] + ", " + names[5] + ", " +
names[6] + "\n"
let date = DateTime.Now
output + String.Format("It is {0:t} on {0:d}. The day of the week is {1}.", date, date.DayOfWeek)
|> printfn "%s"
// The example displays the following output:
// Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
// It is 10:29 AM on 1/8/2018. The day of the week is Monday.
Module Example12
Public Sub Main()
Dim names = {"Balto", "Vanya", "Dakota", "Samuel", "Koani", "Yiska", "Yuma"}
Dim output = names(0) + ", " + names(1) + ", " + names(2) + ", " +
names(3) + ", " + names(4) + ", " + names(5) + ", " +
names(6)
output += vbCrLf
Dim dat = DateTime.Now
output += String.Format("It is {0:t} on {0:d}. The day of the week is {1}.",
dat, dat.DayOfWeek)
Console.WriteLine(output)
End Sub
End Module
' The example displays the following output:
' Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
' It is 10:29 AM on 1/8/2018. The day of the week is Monday.
相比之下,以下示例中的内插字符串的使用比字符串串联语句和对上一示例中方法的调用 Format 更清晰、更简洁。
string[] names = { "Balto", "Vanya", "Dakota", "Samuel", "Koani", "Yiska", "Yuma" };
string output = $"{names[0]}, {names[1]}, {names[2]}, {names[3]}, {names[4]}, " +
$"{names[5]}, {names[6]}";
var date = DateTime.Now;
output += $"\nIt is {date:t} on {date:d}. The day of the week is {date.DayOfWeek}.";
Console.WriteLine(output);
// The example displays the following output:
// Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
// It is 10:29 AM on 1/8/2018. The day of the week is Monday.
open System
let names = [| "Balto"; "Vanya"; "Dakota"; "Samuel"; "Koani"; "Yiska"; "Yuma" |]
let output = $"{names[0]}, {names[1]}, {names[2]}, {names[3]}, {names[4]}, {names[5]}, {names[6]}"
let date = DateTime.Now
output + $"\nIt is {date:t} on {date:d}. The day of the week is {date.DayOfWeek}."
|> printfn "%s"
// The example displays the following output:
// Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
// It is 10:29 AM on 1/8/2018. The day of the week is Monday.
Module Example13
Public Sub Main()
Dim names = {"Balto", "Vanya", "Dakota", "Samuel", "Koani", "Yiska", "Yuma"}
Dim output = $"{names(0)}, {names(1)}, {names(2)}, {names(3)}, {names(4)}, " +
$"{names(5)}, {names(6)}"
Dim dat = DateTime.Now
output += $"{vbCrLf}It is {dat:t} on {dat:d}. The day of the week is {dat.DayOfWeek}."
Console.WriteLine(output)
End Sub
End Module
' The example displays the following output:
' Balto, Vanya, Dakota, Samuel, Koani, Yiska, Yuma
' It is 10:29 AM on 1/8/2018. The day of the week is Monday.
在哪里可以找到预定义的格式字符串?
- 有关所有整型和浮点类型,请参阅 标准数值格式字符串 和 自定义数值格式字符串。
- 有关日期和时间值,请参阅 标准日期和时间格式字符串 以及 自定义日期和时间格式字符串。
- 有关枚举值,请参阅 枚举格式字符串。
- 有关 TimeSpan 值,请参阅 标准 TimeSpan 格式字符串 和 自定义 TimeSpan 格式字符串。
- 有关 Guid 值,请参阅参考页的 Guid.ToString(String) “备注”部分。
如何控制替换格式项的结果字符串的对齐方式?
格式项的一般语法为:
{index[,width][: formatString]}
width 是定义字段宽度的带符号整数。 如果此值为负值,则字段中的文本左对齐。 如果为正数,则文本右对齐。
如何控制小数点分隔符后的位数?
除“D”(仅用于整数)、“G”、“R”和“X”之外的所有 标准数值格式字符串 都允许定义结果字符串中小数位数的精度说明符。 以下示例使用标准数值格式字符串来控制结果字符串中的十进制数字数。
object[] values = { 1603, 1794.68235, 15436.14 };
string result;
foreach (var value in values)
{
result = String.Format("{0,12:C2} {0,12:E3} {0,12:F4} {0,12:N3} {1,12:P2}\n",
Convert.ToDouble(value), Convert.ToDouble(value) / 10000);
Console.WriteLine(result);
}
// The example displays output like the following:
// $1,603.00 1.603E+003 1603.0000 1,603.000 16.03 %
//
// $1,794.68 1.795E+003 1794.6824 1,794.682 17.95 %
//
// $15,436.14 1.544E+004 15436.1400 15,436.140 154.36 %
open System
let values: obj list = [ 1603, 1794.68235, 15436.14 ]
for value in values do
String.Format("{0,12:C2} {0,12:E3} {0,12:F4} {0,12:N3} {1,12:P2}\n", Convert.ToDouble(value), Convert.ToDouble(value) / 10000.)
|> printfn "%s"
// The example displays output like the following:
// $1,603.00 1.603E+003 1603.0000 1,603.000 16.03 %
//
// $1,794.68 1.795E+003 1794.6824 1,794.682 17.95 %
//
// $15,436.14 1.544E+004 15436.1400 15,436.140 154.36 %
Module Example7
Public Sub Main()
Dim values() As Object = {1603, 1794.68235, 15436.14}
Dim result As String
For Each value In values
result = String.Format("{0,12:C2} {0,12:E3} {0,12:F4} {0,12:N3} {1,12:P2}",
value, CDbl(value) / 10000)
Console.WriteLine(result)
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' $1,603.00 1.603E+003 1603.0000 1,603.000 16.03 %
'
' $1,794.68 1.795E+003 1794.6824 1,794.682 17.95 %
'
' $15,436.14 1.544E+004 15436.1400 15,436.140 154.36 %
如果使用 自定义数字格式字符串,请使用“0”格式说明符来控制结果字符串中的十进制数字数,如以下示例所示。
decimal value = 16309.5436m;
string result = String.Format("{0,12:#.00000} {0,12:0,000.00} {0,12:000.00#}",
value);
Console.WriteLine(result);
// The example displays the following output:
// 16309.54360 16,309.54 16309.544
let value = 16309.5436m
String.Format("{0,12:#.00000} {0,12:0,000.00} {0,12:000.00#}", value)
|> printfn "%s"
// The example displays the following output:
// 16309.54360 16,309.54 16309.544
Module Example8
Public Sub Main()
Dim value As Decimal = 16309.5436D
Dim result As String = String.Format("{0,12:#.00000} {0,12:0,000.00} {0,12:000.00#}",
value)
Console.WriteLine(result)
End Sub
End Module
' The example displays the following output:
' 16309.54360 16,309.54 16309.544
如何控制整型数字的位数?
默认情况下,格式操作仅显示非零整数位。 如果要设置整数格式,可以使用精度说明符和“D”和“X”标准格式字符串来控制数字数。
int value = 1326;
string result = String.Format("{0,10:D6} {0,10:X8}", value);
Console.WriteLine(result);
// The example displays the following output:
// 001326 0000052E
open System
let value = 1326
String.Format("{0,10:D6} {0,10:X8}", value)
|> printfn "%s"
// The example displays the following output:
// 001326 0000052E
Module Example10
Public Sub Main()
Dim value As Integer = 1326
Dim result As String = String.Format("{0,10:D6} {0,10:X8}", value)
Console.WriteLine(result)
End Sub
End Module
' The example displays the following output:
' 001326 0000052E
可以使用“0”自定义数字格式说明符在整数或浮点数前添加前导零,从而生成一个具有指定整数位数的结果字符串,如以下示例所示。
int value = 16342;
string result = String.Format("{0,18:00000000} {0,18:00000000.000} {0,18:000,0000,000.0}",
value);
Console.WriteLine(result);
// The example displays the following output:
// 00016342 00016342.000 0,000,016,342.0
open System
let value = 16342
String.Format("{0,18:00000000} {0,18:00000000.000} {0,18:000,0000,000.0}", value)
|> printfn "%s"
// The example displays the following output:
// 00016342 00016342.000 0,000,016,342.0
Module Example9
Public Sub Main()
Dim value As Integer = 16342
Dim result As String = String.Format("{0,18:00000000} {0,18:00000000.000} {0,18:000,0000,000.0}",
value)
Console.WriteLine(result)
End Sub
End Module
' The example displays the following output:
' 00016342 00016342.000 0,000,016,342.0
格式列表中可以包含多少项?
没有实际限制。 Format(IFormatProvider, String, Object[]) 方法的第二个参数被 ParamArrayAttribute 属性标记,该属性允许您将格式列表设定为分隔符列表或对象数组。
如何在结果字符串中包含文本大括号(“{”和“}”) ?
例如,如何阻止以下方法调用引发 FormatException 异常?
result = String.Format("The text has {0} '{' characters and {1} '}' characters.",
nOpen, nClose);
let result =
String.Format("The text has {0} '{' characters and {1} '}' characters.", nOpen, nClose)
result = String.Format("The text has {0} '{' characters and {1} '}' characters.",
nOpen, nClose)
单个左大括号或右大括号始终解释为格式项的开头或结尾。 要按字面解释,就必须对其进行转义。 可以通过添加另一个大括号(“{{”和“}}”代替“{”和“}”)来转义大括号,如下面的方法调用所示:
string result;
int nOpen = 1;
int nClose = 2;
result = String.Format("The text has {0} '{{' characters and {1} '}}' characters.",
nOpen, nClose);
Console.WriteLine(result);
let result =
String.Format("The text has {0} '{{' characters and {1} '}}' characters.", nOpen, nClose)
result = String.Format("The text has {0} '{{' characters and {1} '}}' characters.",
nOpen, nClose)
但是,即使是转义后的大括号也很容易被误读。 建议在格式列表中包括大括号,并使用格式项在结果字符串中插入它们,如以下示例所示。
string result;
int nOpen = 1;
int nClose = 2;
result = String.Format("The text has {0} '{1}' characters and {2} '{3}' characters.",
nOpen, "{", nClose, "}");
Console.WriteLine(result);
let result =
String.Format("The text has {0} '{1}' characters and {2} '{3}' characters.", nOpen, "{", nClose, "}")
result = String.Format("The text has {0} '{1}' characters and {2} '{3}' characters.",
nOpen, "{", nClose, "}")
为什么调用 String.Format 方法会引发 FormatException?
异常的最常见原因是格式项的索引与格式列表中的对象不对应。 通常,这表示你错误地对格式项的索引进行了编号,或者忘记了在格式列表中包括对象。 如果试图包含未转义的左或右大括号字符也会引发 FormatException。 有时,异常是拼写错误的结果;例如,典型的错误是错误键入“[”(左括号),而不是“{”(左大括号)。
如果 Format(System.IFormatProvider,System.String,System.Object[]) 方法支持参数数组,为什么在使用数组时我的代码会引发异常?
例如,以下代码引发异常 FormatException :
Random rnd = new Random();
int[] numbers = new int[4];
int total = 0;
for (int ctr = 0; ctr <= 2; ctr++)
{
int number = rnd.Next(1001);
numbers[ctr] = number;
total += number;
}
numbers[3] = total;
Console.WriteLine($"{numbers} + {1} + {2} = {3}");
open System
let rnd = Random()
let mutable total = 0
let numbers = Array.zeroCreate<int> 4
for i = 0 to 2 do
let number = rnd.Next 1001
numbers[i] <- number
total <- total + number
numbers[3] <- total
Console.WriteLine("{0} + {1} + {2} = {3}", numbers)
Module Example5a
Public Sub Main()
Dim rnd As New Random()
Dim numbers(3) As Integer
Dim total As Integer = 0
For ctr = 0 To 2
Dim number As Integer = rnd.Next(1001)
numbers(ctr) = number
total += number
Next
numbers(3) = total
Console.WriteLine("{0} + {1} + {2} = {3}", numbers)
End Sub
End Module
这是编译器重载解析的问题。 由于编译器无法将整数数组转换为对象数组,因此它将整数数组视为单个参数,因此它会调用 Format(String, Object) 该方法。 引发异常的原因是总共有四个格式项,但格式列表中只有一个项。
由于 Visual Basic 和 C# 都不能将整数数组转换为对象数组,因此必须在调用 Format(String, Object[]) 该方法之前自行执行转换。 以下示例提供一个实现。
Random rnd = new Random();
int[] numbers = new int[4];
int total = 0;
for (int ctr = 0; ctr <= 2; ctr++)
{
int number = rnd.Next(1001);
numbers[ctr] = number;
total += number;
}
numbers[3] = total;
object[] values = new object[numbers.Length];
numbers.CopyTo(values, 0);
Console.WriteLine($"{values} + {1} + {2} = {3}");
open System
let rnd = Random()
let numbers = Array.zeroCreate<int> 4
let mutable total = 0
for i = 0 to 2 do
let number = rnd.Next 1001
numbers[i] <- number
total <- total + number
numbers[3] <- total
let values = Array.zeroCreate<obj> numbers.Length
numbers.CopyTo(values, 0)
Console.WriteLine("{0} + {1} + {2} = {3}", values)
Imports System.Collections.Generic
Module Example6
Public Sub Main()
Dim rnd As New Random()
Dim numbers(3) As Integer
Dim total As Integer = 0
For ctr = 0 To 2
Dim number As Integer = rnd.Next(1001)
numbers(ctr) = number
total += number
Next
numbers(3) = total
Dim values(numbers.Length - 1) As Object
numbers.CopyTo(values, 0)
Console.WriteLine("{0} + {1} + {2} = {3}", values)
End Sub
End Module
注解
Important
如果你的语言支持字符串,则可以使用内插字符串,而不是调用 String.Format 方法或使用复合格式字符串。 内插字符串是包含 内插表达式的字符串。 每个内插表达式都使用表达式的值进行解析,并在分配字符串时包含在结果字符串中。 有关详细信息,请参阅字符串内插(C# 参考)和内插字符串(Visual Basic 参考)。
Format(IFormatProvider, String, Object, Object, Object)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将字符串中的格式项替换为三个指定对象的字符串表示形式。 参数提供区域性特定的格式设置信息。
public:
static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public static string Format(IFormatProvider provider, string format, object arg0, object arg1, object arg2);
public static string Format(IFormatProvider? provider, string format, object? arg0, object? arg1, object? arg2);
static member Format : IFormatProvider * string * obj * obj * obj -> string
Public Shared Function Format (provider As IFormatProvider, format As String, arg0 As Object, arg1 As Object, arg2 As Object) As String
参数
- provider
- IFormatProvider
提供区域性特定的格式设置信息的对象。
- arg0
- Object
要设置格式的第一个对象。
- arg1
- Object
要设置格式的第二个对象。
- arg2
- Object
要设置格式的第三个对象。
返回
例外
format 是 null。
注解
Important
如果你的语言支持字符串,则可以使用内插字符串,而不是调用 String.Format 方法或使用复合格式字符串。 内插字符串是包含 内插表达式的字符串。 每个内插表达式都使用表达式的值进行解析,并在分配字符串时包含在结果字符串中。 有关详细信息,请参阅字符串内插(C# 参考)和内插字符串(Visual Basic 参考)。
此方法使用 复合格式设置功能 将三个表达式转换为其字符串表示形式,并在字符串中嵌入这些表示形式。 在执行转换时,该方法使用区分区域性的格式或自定义格式化程序。 该方法通过调用 Object 方法将每个参数转换为其字符串表示形式,或者,如果对象的相应格式项包含格式字符串,则调用其 ToString(String,IFormatProvider) 方法。 如果这些方法不存在,它将调用对象的无参数 ToString 方法。
但是,调用 String.Format 该方法时,不必专注于要调用的特定重载。 相反,可以使用提供区域性敏感或自定义格式的对象和包含一个或多个 格式项的复合格式字符串 来调用该方法。 为每个格式项分配一个数字索引;第一个索引从 0 开始。 除了初始字符串,方法调用应具有与索引值一样多的其他参数。 例如,格式项的索引为 0 和 1 的字符串应具有 2 个参数;索引为 0 到 5 的 1 个参数应具有 6 个参数。 然后,语言编译器会将方法调用解析为方法的特定重载 String.Format 。
有关使用String.Format该方法的更详细文档,请参阅 String.Format 方法入门以及我调用哪种方法?
适用于
Format(IFormatProvider, String, Object, Object)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将字符串中的格式项替换为两个指定对象的字符串表示形式。 参数提供区域性特定的格式设置信息。
public:
static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public static string Format(IFormatProvider provider, string format, object arg0, object arg1);
public static string Format(IFormatProvider? provider, string format, object? arg0, object? arg1);
static member Format : IFormatProvider * string * obj * obj -> string
Public Shared Function Format (provider As IFormatProvider, format As String, arg0 As Object, arg1 As Object) As String
参数
- provider
- IFormatProvider
提供区域性特定的格式设置信息的对象。
- arg0
- Object
要设置格式的第一个对象。
- arg1
- Object
要设置格式的第二个对象。
返回
一个副本, format 其中格式项替换为字符串表示形式 arg0 和 arg1。
例外
format 是 null。
注解
Important
如果你的语言支持字符串,则可以使用内插字符串,而不是调用 String.Format 方法或使用复合格式字符串。 内插字符串是包含 内插表达式的字符串。 每个内插表达式都使用表达式的值进行解析,并在分配字符串时包含在结果字符串中。 有关详细信息,请参阅字符串内插(C# 参考)和内插字符串(Visual Basic 参考)。
此方法使用 复合格式设置功能 将两个表达式转换为其字符串表示形式,并在字符串中嵌入这些表示形式。 在执行转换时,该方法使用区分区域性的格式或自定义格式化程序。 该方法通过调用 Object 方法将每个参数转换为其字符串表示形式,或者,如果对象的相应格式项包含格式字符串,则调用其 ToString(String,IFormatProvider) 方法。 如果这些方法不存在,它将调用对象的无参数 ToString 方法。
但是,调用 String.Format 该方法时,不必专注于要调用的特定重载。 相反,可以使用提供区域性敏感或自定义格式的对象和包含一个或多个 格式项的复合格式字符串 来调用该方法。 为每个格式项分配一个数字索引;第一个索引从 0 开始。 除了初始字符串,方法调用应具有与索引值一样多的其他参数。 例如,格式项的索引为 0 和 1 的字符串应具有 2 个参数;索引为 0 到 5 的 1 个参数应具有 6 个参数。 然后,语言编译器会将方法调用解析为方法的特定重载 String.Format 。
有关使用String.Format该方法的更详细文档,请参阅 String.Format 方法入门以及我调用哪种方法?
适用于
Format(String, Object, Object)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将字符串中的格式项替换为两个指定对象的字符串表示形式。
public:
static System::String ^ Format(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public static string Format(string format, object arg0, object arg1);
public static string Format(string format, object? arg0, object? arg1);
static member Format : string * obj * obj -> string
Public Shared Function Format (format As String, arg0 As Object, arg1 As Object) As String
参数
- arg0
- Object
要设置格式的第一个对象。
- arg1
- Object
要设置格式的第二个对象。
返回
一个副本, format 其中格式项替换为字符串表示形式 arg0 和 arg1。
例外
format 是 null。
注解
Important
如果你的语言支持字符串,则可以使用内插字符串,而不是调用 String.Format 方法或使用复合格式字符串。 内插字符串是包含 内插表达式的字符串。 每个内插表达式都使用表达式的值进行解析,并在分配字符串时包含在结果字符串中。 有关详细信息,请参阅字符串内插(C# 参考)和内插字符串(Visual Basic 参考)。
此方法使用 复合格式设置功能 将两个表达式的值转换为其字符串表示形式,并在字符串中嵌入这些表示形式。
但是,调用 String.Format 该方法时,不必专注于要调用的特定重载。 相反,可以使用包含一个或多个 格式项的复合格式字符串 调用该方法。 为每个格式项分配一个数字索引;第一个索引从 0 开始。 除了初始字符串,方法调用应具有与索引值一样多的其他参数。 例如,格式项的索引为 0 和 1 的字符串应具有 2 个参数;索引为 0 到 5 的 1 个参数应具有 6 个参数。 然后,语言编译器会将方法调用解析为方法的特定重载 String.Format 。
有关使用String.Format该方法的更详细文档,请参阅 String.Format 方法入门以及我调用哪种方法?
示例:设置两个参数的格式
此示例使用 Format(String, Object, Object) 该方法显示存储在泛型 Dictionary<TKey,TValue> 对象中的时间和温度数据。 请注意,格式字符串有三个格式项,尽管只有两个对象可以设置格式。 这是因为列表中的第一个对象(日期和时间值)由两个格式项使用:第一个格式项显示时间,第二个显示日期。
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
open System
open System.Collections.Generic
let temperatureInfo = Dictionary<DateTime, float>()
temperatureInfo.Add(DateTime(2010, 6, 1, 14, 0, 0), 87.46)
temperatureInfo.Add(DateTime(2010, 12, 1, 10, 0, 0), 36.81)
printfn $"Temperature Information:\n"
for item in temperatureInfo do
String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F", item.Key, item.Value)
|> printfn "%s"
// 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
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim temperatureInfo As New Dictionary(Of Date, Double)
temperatureInfo.Add(#6/1/2010 2:00PM#, 87.46)
temperatureInfo.Add(#12/1/2010 10:00AM#, 36.81)
Console.WriteLine("Temperature Information:")
Console.WriteLine()
Dim output As String
For Each 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)
Next
End Sub
End Module
' The example displays the following output:
' 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
另请参阅
- .NET 中的格式设置类型
- 复合格式设置
- 标准日期和时间格式字符串
- 自定义日期和时间格式字符串
- 标准数字格式字符串
- 自定义数字格式字符串
- 标准 TimeSpan 格式字符串
- 自定义 TimeSpan 格式字符串
- 枚举格式字符串
适用于
Format(IFormatProvider, CompositeFormat, ReadOnlySpan<Object>)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将格式 CompositeFormat 项或项替换为指定格式中相应对象的字符串表示形式。
public:
static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, ReadOnlySpan<System::Object ^> args);
public static string Format(IFormatProvider? provider, System.Text.CompositeFormat format, scoped ReadOnlySpan<object?> args);
public static string Format(IFormatProvider? provider, System.Text.CompositeFormat format, ReadOnlySpan<object?> args);
static member Format : IFormatProvider * System.Text.CompositeFormat * ReadOnlySpan<obj> -> string
Public Shared Function Format (provider As IFormatProvider, format As CompositeFormat, args As ReadOnlySpan(Of Object)) As String
参数
- provider
- IFormatProvider
提供区域性特定的格式设置信息的对象。
- format
- CompositeFormat
- args
- ReadOnlySpan<Object>
要设置格式的对象范围。
返回
带格式的字符串。
例外
format 是 null。
格式项的索引大于或等于提供的参数数。
适用于
Format(IFormatProvider, CompositeFormat, Object[])
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将格式 CompositeFormat 项或项替换为指定格式中相应对象的字符串表示形式。
public:
static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, ... cli::array <System::Object ^> ^ args);
public static string Format(IFormatProvider? provider, System.Text.CompositeFormat format, params object?[] args);
static member Format : IFormatProvider * System.Text.CompositeFormat * obj[] -> string
Public Shared Function Format (provider As IFormatProvider, format As CompositeFormat, ParamArray args As Object()) As String
参数
- provider
- IFormatProvider
提供区域性特定的格式设置信息的对象。
- format
- CompositeFormat
- args
- Object[]
要设置格式的对象数组。
返回
带格式的字符串。
例外
format 或 args 为 null.
格式项的索引大于或等于提供的参数数。
适用于
Format(String, Object, Object, Object)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将字符串中的格式项替换为三个指定对象的字符串表示形式。
public:
static System::String ^ Format(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public static string Format(string format, object arg0, object arg1, object arg2);
public static string Format(string format, object? arg0, object? arg1, object? arg2);
static member Format : string * obj * obj * obj -> string
Public Shared Function Format (format As String, arg0 As Object, arg1 As Object, arg2 As Object) As String
参数
- arg0
- Object
要设置格式的第一个对象。
- arg1
- Object
要设置格式的第二个对象。
- arg2
- Object
要设置格式的第三个对象。
返回
例外
format 是 null。
注解
Important
如果你的语言支持字符串,则可以使用内插字符串,而不是调用 String.Format 方法或使用复合格式字符串。 内插字符串是包含 内插表达式的字符串。 每个内插表达式都使用表达式的值进行解析,并在分配字符串时包含在结果字符串中。 有关详细信息,请参阅字符串内插(C# 参考)和内插字符串(Visual Basic 参考)。
此方法使用 复合格式设置功能 将三个表达式的值转换为其字符串表示形式,并在字符串中嵌入这些表示形式。
但是,调用 String.Format 该方法时,不必专注于要调用的特定重载。 相反,可以使用包含一个或多个 格式项的复合格式字符串 调用该方法。 为每个格式项分配一个数字索引;第一个索引从 0 开始。 除了初始字符串,方法调用应具有与索引值一样多的其他参数。 例如,格式项的索引为 0 和 1 的字符串应具有 2 个参数;索引为 0 到 5 的 1 个参数应具有 6 个参数。 然后,语言编译器会将方法调用解析为方法的特定重载 String.Format 。
有关使用String.Format该方法的更详细文档,请参阅 String.Format 方法入门以及我调用哪种方法?
示例:设置三个参数的格式
此示例使用 Format(String, Object, Object, Object) 该方法创建一个字符串,该字符串演示具有两个整数值的布尔 And 运算的结果。 请注意,格式字符串包含六个格式项,但该方法在其参数列表中只有三个项,因为每个项采用两种不同的格式设置。
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)
open System
let formatString =
" {0,10} ({0,8:X8})\nAnd {1,10} ({1,8:X8})\n = {2,10} ({2,8:X8})"
let value1 = 16932
let value2 = 15421
String.Format(formatString, value1, value2, value1 &&& value2)
|> printfn "%s"
// The example displays the following output:
// 16932 (00004224)
// And 15421 (00003C3D)
// = 36 (00000024)
Public Module Example
Public Sub Main()
Dim formatString As String = " {0,10} ({0,8:X8})" + vbCrLf + _
"And {1,10} ({1,8:X8})" + vbCrLf + _
" = {2,10} ({2,8:X8})"
Dim value1 As Integer = 16932
Dim value2 As Integer = 15421
Dim result As String = String.Format(formatString, _
value1, value2, value1 And value2)
Console.WriteLine(result)
End Sub
End Module
' The example displays the following output:
' 16932 (00004224)
' And 15421 (00003C3D)
' = 36 (00000024)
另请参阅
适用于
Format(IFormatProvider, String, Object[])
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将字符串中的格式项替换为指定数组中相应对象的字符串表示形式。 参数提供区域性特定的格式设置信息。
public:
static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, ... cli::array <System::Object ^> ^ args);
public static string Format(IFormatProvider provider, string format, params object[] args);
public static string Format(IFormatProvider? provider, string format, params object?[] args);
static member Format : IFormatProvider * string * obj[] -> string
Public Shared Function Format (provider As IFormatProvider, format As String, ParamArray args As Object()) As String
参数
- provider
- IFormatProvider
提供区域性特定的格式设置信息的对象。
- args
- Object[]
包含要设置格式的零个或多个对象的对象数组。
返回
一个副本 format ,其中格式项已替换为相应对象的 args字符串表示形式。
例外
format 或 args 为 null.
注解
Important
如果你的语言支持字符串,则可以使用内插字符串,而不是调用 String.Format 方法或使用复合格式字符串。 内插字符串是包含 内插表达式的字符串。 每个内插表达式都使用表达式的值进行解析,并在分配字符串时包含在结果字符串中。 有关详细信息,请参阅字符串内插(C# 参考)和内插字符串(Visual Basic 参考)。
此方法使用 复合格式设置功能 将四个或多个表达式转换为其字符串表示形式,并在字符串中嵌入这些表示形式。 在执行转换时,该方法使用区分区域性的格式或自定义格式化程序。 该方法通过调用 Object 方法将每个参数转换为其字符串表示形式,或者,如果对象的相应格式项包含格式字符串,则调用其 ToString(String,IFormatProvider) 方法。 如果这些方法不存在,它将调用对象的无参数 ToString 方法。
但是,调用 String.Format 该方法时,不必专注于要调用的特定重载。 相反,可以使用提供区域性敏感或自定义格式的对象和包含一个或多个 格式项的复合格式字符串 来调用该方法。 为每个格式项分配一个数字索引;第一个索引从 0 开始。 除了初始字符串,方法调用应具有与索引值一样多的其他参数。 例如,格式项的索引为 0 和 1 的字符串应具有 2 个参数;索引为 0 到 5 的 1 个参数应具有 6 个参数。 然后,语言编译器会将方法调用解析为方法的特定重载 String.Format 。
有关使用String.Format该方法的更详细文档,请参阅 String.Format 方法入门以及我调用哪种方法?
示例:区分区域性的格式
此示例使用 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);
}
// The example displays the following 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
open System
open System.Globalization
let cultureNames = [| "en-US"; "fr-FR"; "de-DE"; "es-ES" |]
let dateToDisplay = DateTime(2009, 9, 1, 18, 32, 0)
let value = 9164.32
printfn "Culture Date Value\n"
for cultureName in cultureNames do
let culture = CultureInfo cultureName
String.Format(culture, "{0,-11} {1,-35:D} {2:N}", culture.Name, dateToDisplay, value)
|> printfn "%s"
// The example displays the following 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
Imports System.Globalization
Module Example2
Public Sub Main()
Dim cultureNames() As String = { "en-US", "fr-FR", "de-DE", "es-ES" }
Dim dateToDisplay As Date = #9/1/2009 6:32PM#
Dim value As Double = 9164.32
Console.WriteLine("Culture Date Value")
Console.WriteLine()
For Each cultureName As String In cultureNames
Dim culture As New CultureInfo(cultureName)
Dim output As String = String.Format(culture, "{0,-11} {1,-35:D} {2:N}", _
culture.Name, dateToDisplay, value)
Console.WriteLine(output)
Next
End Sub
End Module
' The example displays the following 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
另请参阅
- DateTimeFormatInfo
- ICustomFormatter
- IFormatProvider
- NumberFormatInfo
- .NET 中的格式设置类型
- 复合格式设置
- 标准日期和时间格式字符串
- 自定义日期和时间格式字符串
- 标准数字格式字符串
- 自定义数字格式字符串
- 标准 TimeSpan 格式字符串
- 自定义 TimeSpan 格式字符串
- 枚举格式字符串
适用于
Format(IFormatProvider, String, Object)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将指定字符串中的格式项或项替换为相应对象的字符串表示形式。 参数提供区域性特定的格式设置信息。
public:
static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0);
public static string Format(IFormatProvider provider, string format, object arg0);
public static string Format(IFormatProvider? provider, string format, object? arg0);
static member Format : IFormatProvider * string * obj -> string
Public Shared Function Format (provider As IFormatProvider, format As String, arg0 As Object) As String
参数
- provider
- IFormatProvider
提供区域性特定的格式设置信息的对象。
- arg0
- Object
要设置格式的对象。
返回
格式项或项已被字符串表示形式format替换的一个副本arg0。
例外
format 是 null。
注解
Important
如果你的语言支持字符串,则可以使用内插字符串,而不是调用 String.Format 方法或使用复合格式字符串。 内插字符串是包含 内插表达式的字符串。 每个内插表达式都使用表达式的值进行解析,并在分配字符串时包含在结果字符串中。 有关详细信息,请参阅字符串内插(C# 参考)和内插字符串(Visual Basic 参考)。
此方法使用 复合格式设置功能 将表达式的值转换为其字符串表示形式,并将该表示形式嵌入字符串中。 在执行转换时,该方法使用区分区域性的格式或自定义格式化程序。 该方法通过调用 arg0 方法转换为其字符串表示形式,或者,如果对象的相应格式项包含格式字符串,则通过调用其 ToString(String,IFormatProvider) 方法。 如果这些方法不存在,它将调用对象的无参数 ToString 方法。
但是,调用 String.Format 该方法时,不必专注于要调用的特定重载。 相反,可以使用提供区域性敏感或自定义格式的对象和包含一个或多个 格式项的复合格式字符串 来调用该方法。 为每个格式项分配一个数字索引;第一个索引从 0 开始。 除了初始字符串,方法调用应具有与索引值一样多的其他参数。 例如,格式项的索引为 0 和 1 的字符串应具有 2 个参数;索引为 0 到 5 的 1 个参数应具有 6 个参数。 然后,语言编译器会将方法调用解析为方法的特定重载 String.Format 。
有关使用String.Format该方法的更详细文档,请参阅 String.Format 方法入门以及我调用哪种方法?
适用于
Format(String, ReadOnlySpan<Object>)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将指定字符串中的格式项替换为指定范围中相应对象的字符串表示形式。
public:
static System::String ^ Format(System::String ^ format, ReadOnlySpan<System::Object ^> args);
public static string Format(string format, scoped ReadOnlySpan<object?> args);
static member Format : string * ReadOnlySpan<obj> -> string
Public Shared Function Format (format As String, args As ReadOnlySpan(Of Object)) As String
参数
- args
- ReadOnlySpan<Object>
包含要设置格式的零个或多个对象的对象范围。
返回
一个副本 format ,其中格式项已替换为相应对象的 args字符串表示形式。
适用于
Format(String, Object[])
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将指定字符串中的格式项替换为指定数组中相应对象的字符串表示形式。
public:
static System::String ^ Format(System::String ^ format, ... cli::array <System::Object ^> ^ args);
public static string Format(string format, params object[] args);
public static string Format(string format, params object?[] args);
static member Format : string * obj[] -> string
Public Shared Function Format (format As String, ParamArray args As Object()) As String
参数
- args
- Object[]
包含要设置格式的零个或多个对象的对象数组。
返回
一个副本 format ,其中格式项已替换为相应对象的 args字符串表示形式。
例外
format 或 args 为 null.
注解
Important
如果你的语言支持字符串,则可以使用内插字符串,而不是调用 String.Format 方法或使用复合格式字符串。 内插字符串是包含 内插表达式的字符串。 每个内插表达式都使用表达式的值进行解析,并在分配字符串时包含在结果字符串中。 有关详细信息,请参阅字符串内插(C# 参考)和内插字符串(Visual Basic 参考)。
此方法使用 复合格式设置功能 将四个或多个表达式的值转换为其字符串表示形式,并在字符串中嵌入这些表示形式。 由于参数 args 使用 System.ParamArrayAttribute 特性进行标记,因此可以将对象作为单个参数或 Object 数组传递给方法。
但是,调用 String.Format 该方法时,不必专注于要调用的特定重载。 相反,可以使用包含一个或多个 格式项的复合格式字符串 调用该方法。 为每个格式项分配一个数字索引;第一个索引从 0 开始。 除了初始字符串,方法调用应具有与索引值一样多的其他参数。 例如,格式项的索引为 0 和 1 的字符串应具有 2 个参数;索引为 0 到 5 的 1 个参数应具有 6 个参数。 然后,语言编译器会将方法调用解析为方法的特定重载 String.Format 。
有关使用String.Format该方法的更详细文档,请参阅 String.Format 方法入门以及我调用哪种方法?
示例:设置三个以上的参数的格式
此示例创建一个字符串,其中包含特定日期的高温和低温数据。 复合格式字符串在 C# 示例中有五个格式项,Visual Basic 示例中有 6 个格式项。 其中两个格式项定义其相应值的字符串表示形式的宽度,第一个格式项还包括标准日期和时间格式字符串。
DateTime date1 = new DateTime(2009, 7, 1);
TimeSpan hiTime = new TimeSpan(14, 17, 32);
decimal hiTemp = 62.1m;
TimeSpan loTime = new TimeSpan(3, 16, 10);
decimal loTemp = 54.8m;
string result1 = String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)",
date1, hiTime, hiTemp, loTime, loTemp);
Console.WriteLine(result1);
Console.WriteLine();
string result2 = String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)",
new object[] { date1, hiTime, hiTemp, loTime, loTemp });
Console.WriteLine(result2);
// The example displays output like the following:
// Temperature on 7/1/2009:
// 14:17:32: 62.1 degrees (hi)
// 03:16:10: 54.8 degrees (lo)
// Temperature on 7/1/2009:
// 14:17:32: 62.1 degrees (hi)
// 03:16:10: 54.8 degrees (lo)
let date1 = DateTime(2009, 7, 1)
let hiTime = TimeSpan(14, 17, 32)
let hiTemp = 62.1m
let loTime = TimeSpan(3, 16, 10)
let loTemp = 54.8m
String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)", date1, hiTime, hiTemp, loTime, loTemp)
|> printfn "%s\n"
String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)", [| date1 :> obj; hiTime; hiTemp; loTime; loTemp |])
|> printfn "%s"
// The example displays output like the following:
// Temperature on 7/1/2009:
// 14:17:32: 62.1 degrees (hi)
// 03:16:10: 54.8 degrees (lo)
// Temperature on 7/1/2009:
// 14:17:32: 62.1 degrees (hi)
// 03:16:10: 54.8 degrees (lo)
Module Example
Public Sub Main()
Dim date1 As Date = #7/1/2009#
Dim hiTime As New TimeSpan(14, 17, 32)
Dim hiTemp As Decimal = 62.1d
Dim loTime As New TimeSpan(3, 16, 10)
Dim loTemp As Decimal = 54.8d
Dim result1 As String = String.Format("Temperature on {0:d}:{5}{1,11}: {2} degrees (hi){5}{3,11}: {4} degrees (lo)", _
date1, hiTime, hiTemp, loTime, loTemp, vbCrLf)
Console.WriteLine(result1)
Console.WriteLine()
Dim result2 As String = String.Format("Temperature on {0:d}:{5}{1,11}: {2} degrees (hi){5}{3,11}: {4} degrees (lo)", _
New Object() { date1, hiTime, hiTemp, loTime, loTemp, vbCrLf })
Console.WriteLine(result2)
End Sub
End Module
' The example displays the following output:
' Temperature on 7/1/2009:
' 14:17:32: 62.1 degrees (hi)
' 03:16:10: 54.8 degrees (lo)
'
' Temperature on 7/1/2009:
' 14:17:32: 62.1 degrees (hi)
' 03:16:10: 54.8 degrees (lo)
还可以传递要格式化为数组而不是参数列表的对象。
using System;
public class CityInfo
{
public CityInfo(String name, int population, Decimal area, int year)
{
this.Name = name;
this.Population = population;
this.Area = area;
this.Year = year;
}
public readonly String Name;
public readonly int Population;
public readonly Decimal Area;
public readonly int Year;
}
public class FormatExample10
{
public static void Main()
{
CityInfo nyc2010 = new CityInfo("New York", 8175133, 302.64m, 2010);
ShowPopulationData(nyc2010);
CityInfo sea2010 = new CityInfo("Seattle", 608660, 83.94m, 2010);
ShowPopulationData(sea2010);
}
private static void ShowPopulationData(CityInfo city)
{
object[] args = { city.Name, city.Year, city.Population, city.Area };
String result = String.Format("{0} in {1}: Population {2:N0}, Area {3:N1} sq. feet",
args);
Console.WriteLine(result);
}
}
// The example displays the following output:
// New York in 2010: Population 8,175,133, Area 302.6 sq. feet
// Seattle in 2010: Population 608,660, Area 83.9 sq. feet
open System
type CityInfo =
{ Name: string
Population: int
Area: Decimal
Year: int }
let showPopulationData city =
let args: obj[] = [| city.Name; city.Year; city.Population; city.Area |]
String.Format("{0} in {1}: Population {2:N0}, Area {3:N1} sq. feet", args)
|> printfn "%s"
{ Name = "New York"; Population = 8175133; Area = 302.64m; Year = 2010 }
|> showPopulationData
{ Name = "Seattle"; Population = 608660; Area = 83.94m; Year = 2010 }
|> showPopulationData
// The example displays the following output:
// New York in 2010: Population 8,175,133, Area 302.6 sq. feet
// Seattle in 2010: Population 608,660, Area 83.9 sq. feet
Public Class CityInfo
Public Sub New(name As String, population As Integer, area As Decimal, year As Integer)
Me.Name = name
Me.Population = population
Me.Area = area
Me.Year = year
End Sub
Public ReadOnly Name As String
Public ReadOnly Population As Integer
Public ReadOnly Area As Decimal
Public ReadOnly Year As Integer
End Class
Module Example
Public Sub Main()
Dim nyc2010 As New CityInfo("New York", 8175133, 302.64d, 2010)
ShowPopulationData(nyc2010)
Dim sea2010 As New CityInfo("Seattle", 608660, 83.94d, 2010)
ShowPopulationData(sea2010)
End Sub
Private Sub ShowPopulationData(city As CityInfo)
Dim args() As Object = { city.Name, city.Year, city.Population, city.Area }
Dim result = String.Format("{0} in {1}: Population {2:N0}, Area {3:N1} sq. feet", args)
Console.WriteLine(result)
End Sub
End Module
' The example displays the following output:
' New York in 2010: Population 8,175,133, Area 302.6 sq. feet
' Seattle in 2010: Population 608,660, Area 83.9 sq. feet
另请参阅
- .NET 中的格式设置类型
- 复合格式设置
- 标准日期和时间格式字符串
- 自定义日期和时间格式字符串
- 标准数字格式字符串
- 自定义数字格式字符串
- 标准 TimeSpan 格式字符串
- 自定义 TimeSpan 格式字符串
- 枚举格式字符串
适用于
Format(String, Object)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将字符串中的一个或多个格式项替换为指定对象的字符串表示形式。
public:
static System::String ^ Format(System::String ^ format, System::Object ^ arg0);
public static string Format(string format, object arg0);
public static string Format(string format, object? arg0);
static member Format : string * obj -> string
Public Shared Function Format (format As String, arg0 As Object) As String
参数
- arg0
- Object
要设置格式的对象。
返回
其中任何格式项都替换为字符串表示形式format的副本arg0。
例外
format 是 null。
注解
Important
如果你的语言支持字符串,则可以使用内插字符串,而不是调用 String.Format 方法或使用复合格式字符串。 内插字符串是包含 内插表达式的字符串。 每个内插表达式都使用表达式的值进行解析,并在分配字符串时包含在结果字符串中。 有关详细信息,请参阅字符串内插(C# 参考)和内插字符串(Visual Basic 参考)。
此方法使用 复合格式设置功能 将表达式的值转换为其字符串表示形式,并将该表示形式嵌入字符串中。
但是,调用 String.Format 该方法时,不必专注于要调用的特定重载。 相反,可以使用包含一个或多个 格式项的复合格式字符串 调用该方法。 为每个格式项分配一个数字索引;第一个索引从 0 开始。 除了初始字符串,方法调用应具有与索引值一样多的其他参数。 例如,格式项的索引为 0 和 1 的字符串应具有 2 个参数;索引为 0 到 5 的 1 个参数应具有 6 个参数。 然后,语言编译器会将方法调用解析为方法的特定重载 String.Format 。
有关使用String.Format该方法的更详细文档,请参阅 String.Format 方法入门以及我调用哪种方法?
示例:设置单个参数的格式
以下示例使用 Format(String, Object) 该方法在字符串中间嵌入个人年龄。
DateTime birthdate = new DateTime(1993, 7, 28);
DateTime[] dates = { new DateTime(1993, 8, 16),
new DateTime(1994, 7, 28),
new DateTime(2000, 10, 16),
new DateTime(2003, 7, 27),
new DateTime(2007, 5, 27) };
foreach (DateTime dateValue in dates)
{
TimeSpan interval = dateValue - birthdate;
// Get the approximate number of years, without accounting for leap years.
int years = ((int) interval.TotalDays) / 365;
// See if adding the number of years exceeds dateValue.
string output;
if (birthdate.AddYears(years) <= dateValue) {
output = String.Format("You are now {0} years old.", years);
Console.WriteLine(output);
}
else {
output = String.Format("You are now {0} years old.", years - 1);
Console.WriteLine(output);
}
}
// The example displays the following output:
// You are now 0 years old.
// You are now 1 years old.
// You are now 7 years old.
// You are now 9 years old.
// You are now 13 years old.
let birthdate = DateTime(1993, 7, 28)
let dates =
[ DateTime(1993, 8, 16)
DateTime(1994, 7, 28)
DateTime(2000, 10, 16)
DateTime(2003, 7, 27)
DateTime(2007, 5, 27) ]
for dateValue in dates do
let interval = dateValue - birthdate
// Get the approximate number of years, without accounting for leap years.
let years = (int interval.TotalDays) / 365
// See if adding the number of years exceeds dateValue.
if birthdate.AddYears years <= dateValue then
String.Format("You are now {0} years old.", years)
else
String.Format("You are now {0} years old.", years - 1)
|> printfn "%s"
// The example displays the following output:
// You are now 0 years old.
// You are now 1 years old.
// You are now 7 years old.
// You are now 9 years old.
// You are now 13 years old.
Module Example
Public Sub Main()
Dim birthdate As Date = #7/28/1993#
Dim dates() As Date = { #9/16/1993#, #7/28/1994#, #10/16/2000#, _
#7/27/2003#, #5/27/2007# }
For Each dateValue As Date In dates
Dim interval As TimeSpan = dateValue - birthdate
' Get the approximate number of years, without accounting for leap years.
Dim years As Integer = CInt(interval.TotalDays) \ 365
' See if adding the number of years exceeds dateValue.
Dim output As String
If birthdate.AddYears(years) <= dateValue Then
output = String.Format("You are now {0} years old.", years)
Console.WriteLine(output)
Else
output = String.Format("You are now {0} years old.", years - 1)
Console.WriteLine(output)
End If
Next
End Sub
End Module
' The example displays the following output:
' You are now 0 years old.
' You are now 1 years old.
' You are now 7 years old.
' You are now 9 years old.
' You are now 13 years old.
另请参阅
- .NET 中的格式设置类型
- 复合格式设置
- 标准日期和时间格式字符串
- 自定义日期和时间格式字符串
- 标准数字格式字符串
- 自定义数字格式字符串
- 标准 TimeSpan 格式字符串
- 自定义 TimeSpan 格式字符串
- 枚举格式字符串
适用于
Format(IFormatProvider, String, ReadOnlySpan<Object>)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将字符串中的格式项替换为指定范围中相应对象的字符串表示形式。 参数提供区域性特定的格式设置信息。
public:
static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, ReadOnlySpan<System::Object ^> args);
public static string Format(IFormatProvider? provider, string format, scoped ReadOnlySpan<object?> args);
static member Format : IFormatProvider * string * ReadOnlySpan<obj> -> string
Public Shared Function Format (provider As IFormatProvider, format As String, args As ReadOnlySpan(Of Object)) As String
参数
- provider
- IFormatProvider
提供区域性特定的格式设置信息的对象。
- args
- ReadOnlySpan<Object>
包含要设置格式的零个或多个对象的对象范围。
返回
一个副本 format ,其中格式项已替换为相应对象的 args字符串表示形式。
适用于
Format<TArg0,TArg1,TArg2>(IFormatProvider, CompositeFormat, TArg0, TArg1, TArg2)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将格式 CompositeFormat 项或项替换为指定格式中相应对象的字符串表示形式。
public:
generic <typename TArg0, typename TArg1, typename TArg2>
static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, TArg0 arg0, TArg1 arg1, TArg2 arg2);
public static string Format<TArg0,TArg1,TArg2>(IFormatProvider? provider, System.Text.CompositeFormat format, TArg0 arg0, TArg1 arg1, TArg2 arg2);
static member Format : IFormatProvider * System.Text.CompositeFormat * 'TArg0 * 'TArg1 * 'TArg2 -> string
Public Shared Function Format(Of TArg0, TArg1, TArg2) (provider As IFormatProvider, format As CompositeFormat, arg0 As TArg0, arg1 As TArg1, arg2 As TArg2) As String
类型参数
- TArg0
要设置格式的第一个对象的类型。
- TArg1
要设置格式的第二个对象的类型。
- TArg2
要设置格式的第三个对象的类型。
参数
- provider
- IFormatProvider
提供区域性特定的格式设置信息的对象。
- format
- CompositeFormat
- arg0
- TArg0
要设置格式的第一个对象。
- arg1
- TArg1
要设置格式的第二个对象。
- arg2
- TArg2
要设置格式的第三个对象。
返回
带格式的字符串。
例外
format 是 null。
格式项的索引大于或等于提供的参数数。
适用于
Format<TArg0,TArg1>(IFormatProvider, CompositeFormat, TArg0, TArg1)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将格式 CompositeFormat 项或项替换为指定格式中相应对象的字符串表示形式。
public:
generic <typename TArg0, typename TArg1>
static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, TArg0 arg0, TArg1 arg1);
public static string Format<TArg0,TArg1>(IFormatProvider? provider, System.Text.CompositeFormat format, TArg0 arg0, TArg1 arg1);
static member Format : IFormatProvider * System.Text.CompositeFormat * 'TArg0 * 'TArg1 -> string
Public Shared Function Format(Of TArg0, TArg1) (provider As IFormatProvider, format As CompositeFormat, arg0 As TArg0, arg1 As TArg1) As String
类型参数
- TArg0
要设置格式的第一个对象的类型。
- TArg1
要设置格式的第二个对象的类型。
参数
- provider
- IFormatProvider
提供区域性特定的格式设置信息的对象。
- format
- CompositeFormat
- arg0
- TArg0
要设置格式的第一个对象。
- arg1
- TArg1
要设置格式的第二个对象。
返回
带格式的字符串。
例外
format 是 null。
格式项的索引大于或等于提供的参数数。
适用于
Format<TArg0>(IFormatProvider, CompositeFormat, TArg0)
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
- Source:
- String.Manipulation.cs
将格式 CompositeFormat 项或项替换为指定格式中相应对象的字符串表示形式。
public:
generic <typename TArg0>
static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, TArg0 arg0);
public static string Format<TArg0>(IFormatProvider? provider, System.Text.CompositeFormat format, TArg0 arg0);
static member Format : IFormatProvider * System.Text.CompositeFormat * 'TArg0 -> string
Public Shared Function Format(Of TArg0) (provider As IFormatProvider, format As CompositeFormat, arg0 As TArg0) As String
类型参数
- TArg0
要设置格式的第一个对象的类型。
参数
- provider
- IFormatProvider
提供区域性特定的格式设置信息的对象。
- format
- CompositeFormat
- arg0
- TArg0
要设置格式的第一个对象。
返回
带格式的字符串。
例外
format 是 null。
格式项的索引大于或等于提供的参数数。