When I format a string using string.Format()
, I'd like to be able to indicate that I want the value formatted and fill a certain amount of space at the end.
For example, say I have the following:
string.Format("{0}", myStringV开发者_如何学运维alue);
I'd like to be able to tell the Format function to format the value, and if it's less than say 50 characters, fill it (with spaces) so it's 50 characters in length.
Can this be done?
Try this:
string.Format("{0,50}",myStringValue);
or
string.Format("{0,-50}",myStringValue);
What about this:
string.Format("{0}", someString).PadLeft(50, ' ');
or
string.Format("{0}", someString).PadRight(50, ' ');
perhaps have a look at this post:
Pad left or right with string.format (not padleft or padright) with arbitrary string
精彩评论