string format question

Y

Yoavo

Hi,
I want to make a dynamically string format padding on a given string but I
get ABORT when doing it:

string aTest = string.Format("--{0,10}--", "test"); // this is
OK
string aTest2 = string.Format("--{0,-10}--", "test"); // this is
OK too

int aNum = 10;
string aTest3 = string.Format("--{0,-aNum}--", "test"); //
trying to make the padding value dynamically -> ABORT

can someone please help ?

thanks,
Yoav.
 
M

Martin Honnen

Yoavo said:
string aTest2 = string.Format("--{0,-10}--", "test"); // this
is OK too

int aNum = 10;
string aTest3 = string.Format("--{0,-aNum}--", "test"); //
trying to make the padding value dynamically -> ABORT

You can first format the "format string" and then use it e.g. in two steps

int aNum = 10;
string formatString = string.Format("--{{0,-{0}}}--", aNum);
string aTest3 = string.Format(formatString, "test");

or in one step

int aNum = 10;
string aTest3 =
string.Format(string.Format("--{{0,-{0}}}--", aNum), "test");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top