String.Format()方法

String.Format方法是我们在.Net应用开发时经常使用到的,它的灵活使用有时能够达到事半功倍的效果,下面我们就借用MSDN上的一个示例来向大家展示String.Format的各种用法。

该示例展示了Numeric、DateTime和Enumeration标准格式的使用,另外,我们也可以根据需要自定义我们需要的格式。

// ThiscodeexampledemonstratestheString.Format()method.

using System;
class Sample
... {
enumColor...{Yellow=1,Blue,Green};

staticDateTimethisDate=DateTime.Now;

publicstaticvoidMain()
...{
//StoretheoutputoftheString.Formatmethodinastring.
strings="";

//Formatanegativeintegerorfloating-pointnumberinvariousways.
Console.WriteLine("StandardNumericFormatSpecifiers");
s
=String.Format(
"(C)Currency:........{0:C} "+
"(D)Decimal:.........{0:D} "+
"(E)Scientific:.......{1:E} "+
"(F)Fixedpoint:.......{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);

//Formatthecurrentdateinvariousways.
Console.WriteLine("StandardDateTimeFormatSpecifiers");
s
=String.Format(
"(d)Shortdate:.......{0:d} "+
"(D)Longdate:........{0:D} "+
"(t)Shorttime:.......{0:t} "+
"(T)Longtime:........{0:T} "+
"(f)Fulldate/shorttime:..{0:f} "+
"(F)Fulldate/longtime:...{0:F} "+
"(g)Generaldate/shorttime:.{0:g} "+
"(G)Generaldate/longtime:.{0:G} "+
"(default):........{0}(default='G') "+
"(M)Month:..........{0:M} "+
"(R)RFC1123:.........{0:R} "+
"(s)Sortable:........{0:s} "+
"(u)Universalsortable:...{0:u}(invariant) "+
"(U)Universalsortable:...{0:U} "+
"(Y)Year:..........{0:Y} ",
thisDate);
Console.WriteLine(s);

//FormataColorenumerationvalueinvariousways.
Console.WriteLine("StandardEnumerationFormatSpecifiers");
s
=String.Format(
"(G)General:.........{0:G} "+
"(default):........{0}(default='G') "+
"(F)Flags:..........{0:F}(flagsorinteger) "+
"(D)Decimalnumber:.....{0:D} "+
"(X)Hexadecimal:.......{0:X} ",
Color.Green);
Console.WriteLine(s);
}

}
以上代码的输出结果如下:
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: . . . . . . . 2007-1-29
(D) Long date:. . . . . . . . 2007年1月29日
(t) Short time: . . . . . . . 13:57
(T) Long time:. . . . . . . . 13:57:42
(f) Full date/short time: . . 2007年1月29日 13:57
(F) Full date/long time:. . . 2007年1月29日 13:57:42
(g) General date/short time:. 2007-1-29 13:57
(G) General date/long time: . 2007-1-29 13:57:42
(default):. . . . . . . . 2007-1-29 13:57:42 (default = 'G')
(M) Month:. . . . . . . . . . 1月29日
(R) RFC1123:. . . . . . . . . Mon, 29 Jan 2007 13:57:42 GMT
(s) Sortable: . . . . . . . . 2007-01-29T13:57:42
(u) Universal sortable: . . . 2007-01-29 13:57:42Z (invariant)
(U) Universal sortable: . . . 2007年1月29日 5:57:42
(Y) Year: . . . . . . . . . . 2007年1月
Standard Enumeration Format Specifiers
(G) General:. . . . . . . . . Green
(default):. . . . . . . . Green (default = 'G')
(F) Flags:. . . . . . . . . . Green (flags or integer)
(D) Decimal number: . . . . . 3
(X) Hexadecimal:. . . . . . . 00000003
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值