string format question

  • Thread starter Thread starter Yoavo
  • Start date Start date
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.
 
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");
 
Back
Top