开发者

Can someone tell me what this means WriteLine("{0,-12}")

开发者 https://www.devze.com 2022-12-26 09:01 出处:网络
{0,-12} is the part i\'m curious about.. I\'m looking at this example Console.WriteLine(\"{0,-12} {1}\", sqlReader.GetName(0),

{0,-12} is the part i'm curious about..

I'm looking at this example

    Console.WriteLine("{0,-12} {1}", sqlReader.GetName(0),
                                    开发者_开发知识库     sqlReader.GetName(1));

Cheers :)


The "0" part of "{0,-12}" says to take the first argument (sqlReader.GetName(0)). The "-12" part indicates that the string should be left-aligned, and that it should use 12 spaces (the field width). If the data doesn't use all 12 spaces, it will fill in the remaining spaces to make the string a total width of 12.

You can see all the options here: http://msdn.microsoft.com/en-us/library/txafckwd.aspx


from msdn

{index[,length][:formatString]}

length: The minimum number of characters in the string representation of the parameter. If positive, the parameter is right-aligned; if negative, it is left-aligned.


It is for string alignment.

http://www.csharp-examples.net/align-string-with-spaces/


The -12 part of the format specifier tells the formatter to write the content in a space 12 characters wide with left-justification. If the content is fewer than 12 characters, the rightmost positions will be filled with spaces. IF it's more than 12 characters, the text will just spill over. I'm guessing that the example is trying to make neatly formatted columnar data:

0123456789012345678901234567890
ShortText   OtherData
LongerText  OtherData
ReallyLongTextOtherData
0

精彩评论

暂无评论...
验证码 换一张
取 消