www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Formatting strings into columns via format?

Since D is a lot more flexible than C, I was wondering if there is an elegant
way to format data into certain columns, no matter how wide the strings actually
are:

   printf("   \"%2d\"   \"%-32.*s\"   \"%-16.*s\"\n", 
            game_weapcorr[j], game_list[j], game_weap[j]);

yields e.g.:

   " 1"   " killed   by MOD_GAUNTLET       "   "Gauntlet        "

As one can see, the trailing "blanks" are a bit sub optimal. I'd perfer to
format things like this:

    "1"   " killed   by MOD_GAUNTLET"          "Gauntlet"

Question: Is that possible using format parameters?


I used to add calculated space() numbers to fix this:

   printf("   \"%2d\" %s \"%.*s\" %s \"%.*s\"\n", 
          game_weapcorr[j], space(32-game_weapcorr.length),
          game_list[j], space(16-game_list.length),
          game_weap[j]);

But that is not really elegant. Any ideas?

AEon
Mar 22 2005