Randy Minder
Randy Minder

Reputation: 48392

String.Format() question

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}", myStringValue);

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?

Upvotes: 1

Views: 907

Answers (3)

Zann Anderson
Zann Anderson

Reputation: 4897

What about this:

string.Format("{0}", someString).PadLeft(50, ' ');

or

string.Format("{0}", someString).PadRight(50, ' ');

Upvotes: 2

Rob
Rob

Reputation: 10248

perhaps have a look at this post:

Pad left or right with string.format (not padleft or padright) with arbitrary string

Upvotes: 1

Stormenet
Stormenet

Reputation: 26468

Try this:

string.Format("{0,50}",myStringValue);

or

string.Format("{0,-50}",myStringValue);

Upvotes: 10

Related Questions