C# String.Format 方法

本文展示了 C# 中 String.Format 方法的应用示例,包括数值、日期时间和枚举类型的格式化输出。

using  System;
class  Sample 
{
    
enum Color {Yellow = 1, Blue, Green};
    
static DateTime thisDate = DateTime.Now;

    
public static void Main() 
    
{
// Store the output of the String.Format method in a string.
    string s = "";

    Console.Clear();

// Format a negative integer or floating-point number in various ways.
    Console.WriteLine("Standard Numeric Format Specifiers");
    s 
= String.Format(
        
"(C) Currency: . . . . . . . . {0:C} " +
        
"(D) Decimal:. . . . . . . . . {0:D} " +
        
"(E) Scientific: . . . . . . . {1:E} " +
        
"(F) Fixed point:. . . . . . . {1:F} " +
        
"(G) General:. . . . . . . . . {0:G} " +
        
"    (default):. . . . . . . . {0} (default = 'G') " +
        
"(N) Number: . . . . . . . . . {0:N} " +
        
"(P) Percent:. . . . . . . . . {1:P} " +
        
"(R) Round-trip: . . . . . . . {1:R} " +
        
"(X) Hexadecimal:. . . . . . . {0:X} ",
        
-123-123.45f); 
    Console.WriteLine(s);

// Format the current date in various ways.
    Console.WriteLine("Standard DateTime Format Specifiers");
    s 
= String.Format(
        
"(d) Short date: . . . . . . . {0:d} " +
        
"(D) Long date:. . . . . . . . {0:D} " +
        
"(t) Short time: . . . . . . . {0:t} " +
        
"(T) Long time:. . . . . . . . {0:T} " +
        
"(f) Full date/short time: . . {0:f} " +
        
"(F) Full date/long time:. . . {0:F} " +
        
"(g) General date/short time:. {0:g} " +
        
"(G) General date/long time: . {0:G} " +
        
"    (default):. . . . . . . . {0} (default = 'G') " +
        
"(M) Month:. . . . . . . . . . {0:M} " +
        
"(R) RFC1123:. . . . . . . . . {0:R} " +
        
"(s) Sortable: . . . . . . . . {0:s} " +
        
"(u) Universal sortable: . . . {0:u} (invariant) " +
        
"(U) Universal sortable: . . . {0:U} " +
        
"(Y) Year: . . . . . . . . . . {0:Y} "
        thisDate);
    Console.WriteLine(s);

// Format a Color enumeration value in various ways.
    Console.WriteLine("Standard Enumeration Format Specifiers");
    s 
= String.Format(
        
"(G) General:. . . . . . . . . {0:G} " +
        
"    (default):. . . . . . . . {0} (default = 'G') " +
        
"(F) Flags:. . . . . . . . . . {0:F} (flags or integer) " +
        
"(D) Decimal number: . . . . . {0:D} " +
        
"(X) Hexadecimal:. . . . . . . {0:X} "
        Color.Green);       
    Console.WriteLine(s);
    }

}

/*
This code example produces the following results:

Standard Numeric Format Specifiers
(C) Currency: . . . . . . . . ($123.00)
(D) Decimal:. . . . . . . . . -123
(E) Scientific: . . . . . . . -1.234500E+002
(F) Fixed point:. . . . . . . -123.45
(G) General:. . . . . . . . . -123
    (default):. . . . . . . . -123 (default = 'G')
(N) Number: . . . . . . . . . -123.00
(P) Percent:. . . . . . . . . -12,345.00 %
(R) Round-trip: . . . . . . . -123.45
(X) Hexadecimal:. . . . . . . FFFFFF85

Standard DateTime Format Specifiers
(d) Short date: . . . . . . . 6/26/2004
(D) Long date:. . . . . . . . Saturday, June 26, 2004
(t) Short time: . . . . . . . 8:11 PM
(T) Long time:. . . . . . . . 8:11:04 PM
(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
(g) General date/short time:. 6/26/2004 8:11 PM
(G) General date/long time: . 6/26/2004 8:11:04 PM
    (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
(M) Month:. . . . . . . . . . June 26
(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM
(Y) Year: . . . . . . . . . . June, 2004

Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
    (default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003

String.Format 方法 (String, Object)

 

 

将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项。

命名空间:System
程序集:mscorlib(在 mscorlib.dll 中)

C#
public static string Format (
    string format,
    Object arg0
)

 

参数
format

String,包含零个或多个格式项。

arg0

要格式化的 Object

 

 

返回值
format 的一个副本,其中的第一个格式项已替换为 arg0String 等效项。
异常类型条件

ArgumentNullException

format 为 空引用(在 Visual Basic 中为 Nothing)。

FormatException

format 中的格式项无效。

- 或 -

用来表示要格式化的参数的数字小于零,或者大于或等于要格式化的指定对象的数目。

此方法使用 .NET Framework 的复合格式设置功能将对象的值转换为其文本表示形式,并将该表示形式嵌入字符串中。.NET Framework 提供了广泛的格式设置支持,下面的格式设置主题中对此有更详细的描述。

format 参数由零个或多个文本序列与零个或多个索引占位符混合组成,其中索引占位符称为格式项,它们与此方法的参数列表中的对象相对应。格式设置过程将每个格式项替换为对应对象的值的文本表示形式。

格式项的语法是 {index[,alignment][:formatString]},它指定了一个强制索引、格式化文本的可选长度和对齐方式,以及格式说明符字符的可选字符串,其中格式说明符字符用于控制如何设置相应对象的值的格式。格式项的组成部分包括:

index

从零开始的整数,指示对象列表中要格式化的元素。如果由 index 指定的对象是 空引用(在 Visual Basic 中为 Nothing),则格式项将被空字符串 ("") 替换。

alignment

可选整数,指示包含格式化值的区域的最小宽度。如果格式化值的长度小于 alignment,则用空格填充该区域。如果 alignment 为负,则格式化的值将在该区域中左对齐;如果 alignment 为正,则格式化的值将右对齐。如果没有指定 alignment,则该区域的长度为格式化值的长度。如果指定 alignment,则需使用逗号。

formatString

可选的格式说明符字符串。如果没有指定 formatString,并且对应的参数实现了 IFormattable 接口,则将 空引用(在 Visual Basic 中为 Nothing) 用作 IFormattable.ToString 格式字符串。因此,IFormattable.ToString 的所有实现都必须允许 空引用(在 Visual Basic 中为 Nothing) 作为格式字符串,并以 String 对象的形式返回对象表示形式的默认格式设置。如果指定 formatString,则必须使用冒号。

必须使用前导大括号字符和后缀大括号字符,即“{”和“}”。若要在 format 中指定单个大括号字符,请指定两个前导大括号字符或后缀大括号字符(即“{{”或“}}”)。

如果 format 的值为“Thank you for your purchase of {0:####} copies of Microsoft®.NET (Core Reference).”,并且 arg0 是值为 123 的 Int16,则返回值为:

“Thank you for your purchase of 123 copies of Microsoft®.NET (Core Reference).”

如果 format 的值为“Brad's dog has {0,-8:G} fleas.”,arg0 是值为 42 的 Int16(在此示例中,下划线表示填充空格),则返回值将为:

“Brad's dog has 42______ fleas.”

下面的代码示例演示数字、日期和枚举的标准格式设置说明符。

c#
// This code example demonstrates the String.Format() method.
// Formatting for this example uses the "en-US" culture.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值