String.Format 格式
Format | Aufruf | Ergebnis |
Zahlen mit vordefiniertem Format formatieren (zahl = 1000000) | ||
c | String.Format("{0:c}", zahl) | 1.000.000.00 ? |
d | String.Format("{0:d}", zahl) | 1000000 |
e | String.Format("{0:e}", zahl) | 1000000e+006 |
f | String.Format("{0:f}", zahl) | 1000000,00 |
g | String.Format("{0:g}", zahl) | 1000000 |
n | String.Format("{0:n}", zahl) | 1.000.000.00 |
x | String.Format("{0:x}", zahl) | f4240 |
Zahlen mit eigenem Format formatieren (zahl = 1000000) | ||
0 | String.Format("{0:00.0000}", zahl) | 1000000,0000 |
# | String.Format("{0:(#).##}", zahl) | (1000000) |
. | String.Format("{0:0.0}", zahl) | 1000000,0 |
, | String.Format("{0:0,0}", zahl) | 1.000.000 |
,. | String.Format("{0:0,.}", zahl) | 1000 |
% | String.Format("{0:0%}", zahl) | 100000000% |
e | String.Format("{0:00e+0}", zahl) | 10e+5 |
Datum mit vordefiniertem Format formatieren | ||
u | String.Format("{0:u}", DateTime.Now) | 2003-12-03 14:43:36Z |
U | String.Format("{0:U}", DateTime.Now) | Mittwoch, 3. Dezember 2003 13:43:36 |
r | String.Format("{0:r}", DateTime.Now) | Wed, 03 Dec 2003 14:43:36 GMT |
s | String.Format("{0:s}", DateTime.Now) | 2003-12-03T14:43:36 |
Y | String.Format("{0:Y}", DateTime.Now) | Dezember 2003 |
g | String.Format("{0:g}", DateTime.Now) | 03.12.2003 14:43 |
G | String.Format("{0:G}", DateTime.Now) | 03.12.2003 14:43:36 |
M | String.Format("{0:M}", DateTime.Now) | 03 Dezember |
d | String.Format("{0:d}", DateTime.Now) | 03.12.2003 |
D | String.Format("{0:D}", DateTime.Now) | Mittwoch, 3. Dezember 2003 |
t | String.Format("{0:t}", DateTime.Now) | 14:43 |
T | String.Format("{0:T}", DateTime.Now) | 14:43:36 |
f | String.Format("{0:f}", DateTime.Now) | Mittwoch, 3. Dezember 2003 14:43 |
F | String.Format("{0:F}", DateTime.Now) | Mittwoch, 3. Dezember 2003 14:43:36 |
Datum mit eigenem Format formatieren | ||
dd | String.Format("{0:F}", DateTime.Now) | 03 |
ddd | String.Format("{0:F}", DateTime.Now) | Mi |
dddd | String.Format("{0:F}", DateTime.Now) | Mittwoch |
MM | String.Format("{0:F}", DateTime.Now) | 12 |
MMM | String.Format("{0:F}", DateTime.Now) | Dez |
MMMM | String.Format("{0:F}", DateTime.Now) | Dezember |
yy | String.Format("{0:F}", DateTime.Now) | 03 |
yyyy | String.Format("{0:F}", DateTime.Now) | 2003 |
ss | String.Format("{0:F}", DateTime.Now) | 36 |
mm | String.Format("{0:F}", DateTime.Now) | 43 |
hh | String.Format("{0:F}", DateTime.Now) | 02 |
HH | String.Format("{0:F}", DateTime.Now) | 14 |
gg | String.Format("{0:F}", DateTime.Now) | n. Chr. |
tt | String.Format("{0:F}", DateTime.Now) | |
zz | String.Format("{0:F}", DateTime.Now) | +01 |
zzz | String.Format("{0:F}", DateTime.Now) | +01:00 |
1// 这个例子演示 String.Format() 方法
2
3
4using System;
5class Sample
6{
7 enum Color {Yellow = 1, Blue, Green};
8 static DateTime thisDate = DateTime.Now;
9
10 public static void Main()
11 {
12
13 //存储 String.Format 方法的输出字符串
14 string s = "";
15
16 Console.Clear();
17
18
19 // 格式化一个负数或者浮点数以不同的方式
20 Console.WriteLine("Standard Numeric Format Specifiers");
21 s = String.Format(
22 "(C) Currency: . . . . . . . . {0:C}\n" +
23 "(D) Decimal:. . . . . . . . . {0:D}\n" +
24 "(E) Scientific: . . . . . . . {1:E}\n" +
25 "(F) Fixed point:. . . . . . . {1:F}\n" +
26 "(G) General:. . . . . . . . . {0:G}\n" +
27 " (default):. . . . . . . . {0} (default = 'G')\n" +
28 "(N) Number: . . . . . . . . . {0:N}\n" +
29 "(P) Percent:. . . . . . . . . {1:P}\n" +
30 "(R) Round-trip: . . . . . . . {1:R}\n" +
31 "(X) Hexadecimal:. . . . . . . {0:X}\n",
32 -123, -123.45f);
33 Console.WriteLine(s);
34
35// Format the current date in various ways.
36 Console.WriteLine("Standard DateTime Format Specifiers");
37 s = String.Format(
38 "(d) Short date: . . . . . . . {0:d}\n" +
39 "(D) Long date:. . . . . . . . {0:D}\n" +
40 "(t) Short time: . . . . . . . {0:t}\n" +
41 "(T) Long time:. . . . . . . . {0:T}\n" +
42 "(f) Full date/short time: . . {0:f}\n" +
43 "(F) Full date/long time:. . . {0:F}\n" +
44 "(g) General date/short time:. {0:g}\n" +
45 "(G) General date/long time: . {0:G}\n" +
46 " (default):. . . . . . . . {0} (default = 'G')\n" +
47 "(M) Month:. . . . . . . . . . {0:M}\n" +
48 "(R) RFC1123:. . . . . . . . . {0:R}\n" +
49 "(s) Sortable: . . . . . . . . {0:s}\n" +
50 "(u) Universal sortable: . . . {0:u} (invariant)\n" +
51 "(U) Universal sortable: . . . {0:U}\n" +
52 "(Y) Year: . . . . . . . . . . {0:Y}\n",
53 thisDate);
54 Console.WriteLine(s);
55
56// Format a Color enumeration value in various ways.
57 Console.WriteLine("Standard Enumeration Format Specifiers");
58 s = String.Format(
59 "(G) General:. . . . . . . . . {0:G}\n" +
60 " (default):. . . . . . . . {0} (default = 'G')\n" +
61 "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
62 "(D) Decimal number: . . . . . {0:D}\n" +
63 "(X) Hexadecimal:. . . . . . . {0:X}\n",
64 Color.Green);
65 Console.WriteLine(s);
66 }
67}
68
69/*
70执行的结果
71
72Standard Numeric Format Specifiers
73(C) Currency: . . . . . . . . ($123.00)
74(D) Decimal:. . . . . . . . . -123
75(E) Scientific: . . . . . . . -1.234500E+002
76(F) Fixed point:. . . . . . . -123.45
77(G) General:. . . . . . . . . -123
78 (default):. . . . . . . . -123 (default = 'G')
79(N) Number: . . . . . . . . . -123.00
80(P) Percent:. . . . . . . . . -12,345.00 %
81(R) Round-trip: . . . . . . . -123.45
82(X) Hexadecimal:. . . . . . . FFFFFF85
83
84Standard DateTime Format Specifiers
85(d) Short date: . . . . . . . 6/26/2004
86(D) Long date:. . . . . . . . Saturday, June 26, 2004
87(t) Short time: . . . . . . . 8:11 PM
88(T) Long time:. . . . . . . . 8:11:04 PM
89(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
90(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
91(g) General date/short time:. 6/26/2004 8:11 PM
92(G) General date/long time: . 6/26/2004 8:11:04 PM
93 (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
94(M) Month:. . . . . . . . . . June 26
95(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
96(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
97(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
98(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM
99(Y) Year: . . . . . . . . . . June, 2004
100
101Standard Enumeration Format Specifiers
102(G) General:. . . . . . . . . Green
103 (default):. . . . . . . . Green (default = 'G')
104(F) Flags:. . . . . . . . . . Green (flags or integer)
105(D) Decimal number: . . . . . 3
106(X) Hexadecimal:. . . . . . . 00000003
107
108*/
109
2
3
4using System;
5class Sample
6{
7 enum Color {Yellow = 1, Blue, Green};
8 static DateTime thisDate = DateTime.Now;
9
10 public static void Main()
11 {
12
13 //存储 String.Format 方法的输出字符串
14 string s = "";
15
16 Console.Clear();
17
18
19 // 格式化一个负数或者浮点数以不同的方式
20 Console.WriteLine("Standard Numeric Format Specifiers");
21 s = String.Format(
22 "(C) Currency: . . . . . . . . {0:C}\n" +
23 "(D) Decimal:. . . . . . . . . {0:D}\n" +
24 "(E) Scientific: . . . . . . . {1:E}\n" +
25 "(F) Fixed point:. . . . . . . {1:F}\n" +
26 "(G) General:. . . . . . . . . {0:G}\n" +
27 " (default):. . . . . . . . {0} (default = 'G')\n" +
28 "(N) Number: . . . . . . . . . {0:N}\n" +
29 "(P) Percent:. . . . . . . . . {1:P}\n" +
30 "(R) Round-trip: . . . . . . . {1:R}\n" +
31 "(X) Hexadecimal:. . . . . . . {0:X}\n",
32 -123, -123.45f);
33 Console.WriteLine(s);
34
35// Format the current date in various ways.
36 Console.WriteLine("Standard DateTime Format Specifiers");
37 s = String.Format(
38 "(d) Short date: . . . . . . . {0:d}\n" +
39 "(D) Long date:. . . . . . . . {0:D}\n" +
40 "(t) Short time: . . . . . . . {0:t}\n" +
41 "(T) Long time:. . . . . . . . {0:T}\n" +
42 "(f) Full date/short time: . . {0:f}\n" +
43 "(F) Full date/long time:. . . {0:F}\n" +
44 "(g) General date/short time:. {0:g}\n" +
45 "(G) General date/long time: . {0:G}\n" +
46 " (default):. . . . . . . . {0} (default = 'G')\n" +
47 "(M) Month:. . . . . . . . . . {0:M}\n" +
48 "(R) RFC1123:. . . . . . . . . {0:R}\n" +
49 "(s) Sortable: . . . . . . . . {0:s}\n" +
50 "(u) Universal sortable: . . . {0:u} (invariant)\n" +
51 "(U) Universal sortable: . . . {0:U}\n" +
52 "(Y) Year: . . . . . . . . . . {0:Y}\n",
53 thisDate);
54 Console.WriteLine(s);
55
56// Format a Color enumeration value in various ways.
57 Console.WriteLine("Standard Enumeration Format Specifiers");
58 s = String.Format(
59 "(G) General:. . . . . . . . . {0:G}\n" +
60 " (default):. . . . . . . . {0} (default = 'G')\n" +
61 "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
62 "(D) Decimal number: . . . . . {0:D}\n" +
63 "(X) Hexadecimal:. . . . . . . {0:X}\n",
64 Color.Green);
65 Console.WriteLine(s);
66 }
67}
68
69/*
70执行的结果
71
72Standard Numeric Format Specifiers
73(C) Currency: . . . . . . . . ($123.00)
74(D) Decimal:. . . . . . . . . -123
75(E) Scientific: . . . . . . . -1.234500E+002
76(F) Fixed point:. . . . . . . -123.45
77(G) General:. . . . . . . . . -123
78 (default):. . . . . . . . -123 (default = 'G')
79(N) Number: . . . . . . . . . -123.00
80(P) Percent:. . . . . . . . . -12,345.00 %
81(R) Round-trip: . . . . . . . -123.45
82(X) Hexadecimal:. . . . . . . FFFFFF85
83
84Standard DateTime Format Specifiers
85(d) Short date: . . . . . . . 6/26/2004
86(D) Long date:. . . . . . . . Saturday, June 26, 2004
87(t) Short time: . . . . . . . 8:11 PM
88(T) Long time:. . . . . . . . 8:11:04 PM
89(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM
90(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM
91(g) General date/short time:. 6/26/2004 8:11 PM
92(G) General date/long time: . 6/26/2004 8:11:04 PM
93 (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G')
94(M) Month:. . . . . . . . . . June 26
95(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT
96(s) Sortable: . . . . . . . . 2004-06-26T20:11:04
97(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant)
98(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM
99(Y) Year: . . . . . . . . . . June, 2004
100
101Standard Enumeration Format Specifiers
102(G) General:. . . . . . . . . Green
103 (default):. . . . . . . . Green (default = 'G')
104(F) Flags:. . . . . . . . . . Green (flags or integer)
105(D) Decimal number: . . . . . 3
106(X) Hexadecimal:. . . . . . . 00000003
107
108*/
109