开发者

String.Format() question

开发者 https://www.devze.com 2023-02-21 20:09 出处:网络
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.

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

0

精彩评论

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