Oft kommt 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:
第二部分: String.Format("{0}", "formatting string"}; To compare string formatting in C# to those in C lets have an example, char szOutput[256];
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);
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 example output
specifier type format output
String.Format("{0:£#,##0.00;(£#,##0.00);Nothing}", value);
date formatting specifier type output
culture information |
String.Format格式化字符串一览
最新推荐文章于 2021-11-26 19:52:55 发布
Zeichenketten und Datumswerte formatieren