String. Format formatted string list

Source: Internet
Author: User
Tags month name
Zeichenketten und datumswerte formatieren

Oft komt es vor, dass man zeichenketten und datumswerte in einem bestimmten format ausgeben muss. das Microsoft framework stellt eine mächte methode zur Verf ügung mit der man einfach und Schnell formatvorlagen auf zeichenketten oder datumswerte anwenden kann: string. format ().

Im folgenden findet man die wichtigtens formate:

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) 000000e + 006
F String. Format ("{0: f}", Zahl) 000000,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) 000000,0000
# String. Format ("{0 :( #). ##}", Zahl) (1000000)
. String. Format ("{0. 0}", Zahl) 1000000,0
, String. Format ("{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
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
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) + 0: 00

Part 2:

String. Format ("{0}", "formatting string "};
Tue, 14 Jul 2004 19:30:00 GMT
One of the painful things about good old ASP was string formatting, VBScript simply didn't have anything useful. C # (and VB. net) do, But msdn doesn' t provide a quick reference to the formatting options. so here's a quick reference.

To compare string formatting in C # to those in C lets have an example,

Char szoutput [256];
Sprintf (szoutput, "At loop position % d. \ n", I );

Sprintf takes an output buffer, a format string and any number of arguments to substitute into the format string.

The C # equivalent for sprintf is string. format, which takes a format string and the arguments. it returns a string, and because you're not passing in a buffer there's no chance of a buffer overflow.

String outputstring = string. Format ("at loop position {0}. \ n", I );

So why doesn't have the format argument have parameters specifying what data type you're about formatting? The CLR objects have metadata which informs the CLR what the objects are, and each object has a standard tostring () method which returns a string representation of that object. much nicer than C where if you passed the wrong type of variable into sprintf everything cocould come crashing down.

The tostring method can accept a string parameter which tells the object how to format itself. in the call to string. format, the formatting string is passed after the position, for example, "{0 :##}". the text inside the curly braces is {argumentindex [, alignment] [: formatstring]}. if alignment is positive, the text is right-padding to fill the specified field length, if it's negative, it's left-padded.

Formatting strings
There's not much formatting that can be applied to a string. Only the padding/alignment formatting options can be applied. These options are also available to every argument, regardless of type.

Example output
String. Format ("-- {1, 10} --", "test"); -- test --
String. Format ("-- {1,-10} --", "test"); -- test --

Formatting numbers
Number formatting is culture dependant. for example, formatting a currency string on my laptop will return a result like £9. 99, formatting a currency on a machine set for the US region wowould return $9.99.

Specifier Type format output
(Double, 1.2345) Output
(INT-1, 12345)
C currency {0: c} £ 1. 23-£ 12,345.00
D decimal
(Whole number) {0: d} system. formatexception-12345
E exponent/scientific {0: e} 1.234500e + 000-1.234500e + 004
F fixed point {0: f} 1.23-12345.00
G General {0: g} 1.2345-12345
N number {0: n} 1.23-12,345.00
R round trippable {0: R} 1.23 system. formatexception
X hexadecimal {0: X4} system. formatexception ffffcfc7

Custom number formatting
Specifier Type format output
(Double 1, 1234.56)
0 zero placeholder {0: 00. 000} 1234.560
# Digit placeholder {0: ##. ##} 1234.56
. Decimal point placeholder {0: 0. 0} 1234.6
, Thousand separator {0: 0, 0} 1,235
% Percentage {0: 0%} 123456%

In addition there is the group separator; this is useful for varying the format, depending on the value of the parameter passed. For example

String. Format ("{0: £#,## 0.00; (£#,## 0.00); nothing}", value );

This will output "£ 1,240.00" if passed 1243.56. it will output the same format bracketed if the value is negative "(£1,240.00)", and will output the string "nothing" if the number is zero.

Date formatting
Date formats are very dependant on the Culture Information passed. The examples below are shown using the UK culture.

specifier type output
(June 8, 1970 12:30:59)
d short date 08/06/1970
D long date 08 June 1970
T short time 12:30:59
T long time
F full date and time 08 June 1970
F full date and time (long) 08 June 1970 12:30:59
G default date and time 08/06/1970
G default date and time (long) 08/06/1970 12:30:59
M day/month 8 June
r rfc1123 date string Mon, 08 Jun 1970 12:30:59 GMT
S sortable date/time 1970-06-08t12: 30: 59
U Universal Time, local timezone 1970-06-08 12: 30: 59z
Y month/year June 1970

Custom date formatting
Specifier type output
(June 8, 1970 12:30:59)
Dd Day 08
Ddd short day name mon
Dddd full day name Monday
HH 2 digit hour 12
HH 2 digit hour (24 hour) 12
Mm 2 digit minute 30
Mm Month 06
Mmm short month name Jun
Mmmm month name June
SS seconds 59
Tt am/PM
YY 2 digit year 70
Yyyy4 digit year 1970
: Seperator, e.g. {0: hh: mm: SS} 12:30:59
/Seperator, e.g. {0: dd/mm/yyyy} 08/06/1970

There are others, including time zone formatting and so on, but the ones above are the most commonly used.

Culture Information
string. format also provides a method which accepts a cultureinfo argument, as an iformatprovider. this is important when trying to write portable and localisable code, as, for example, month names will change according to the local culture of the machine you are running on. rather than simply call the standard string. format you shoshould consider always calling the overloaded culture method. if you don't need to specify a culture you can use the system. globalization. cultureinfo. invariantculture. this will then default your formatting to English, as opposed to the culture of the current thread.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.