Formatting a string with string.Format(“{0:00}”

巧了我就是萌 提交于 2019-12-03 09:39:59

The first 0 is the placeholder, means the first parameter. 00 is an actual format.

For example it could be like this:

var result = string.Format("{0:00} - {1:00}", 5, 6);

result will be 05 - 06. So the first 0 is means take the first parameter 5, while 1 means to take parameter 6.

The format is {index[,length][:formatString]}. Take a look at String.Format Method.

The first 0 in the following line is for the index of your argument

string.Format("{0:00}", int.Parse(testVal) + 1); 

(int.Parse(testVal) + 1).ToString ("00") will yield the same thing.

string.Format supports multiple substitutions like this:

string.Format("{0:00} + 1 = {1:00}", int.Parse(testVal), int.Parse(testVal) + 1); 

The leading 0 in the format string {0:00} indicates the index of this formatter specification. The MSDN documentation for String.Format has this to say about the index...

index: The zero-based position in the parameter list of the object to be formatted.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!