How to use `format` to repeat a character

Meta via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jul 11 09:06:43 PDT 2016


On Monday, 11 July 2016 at 09:02:12 UTC, Bahman Movaqar wrote:
> I'm sure I'm missing something very simple but how can I create 
> a string
> like "----" using `format`?
> I check the docs on `format` and tried many variations including
> `format("%.*c\n", 4, '-')` but got nowhere.
>
> I'd appreciate any hint/help on this.

There's at least one way to do this, by using position arguments 
and repeating it the desired number of times in the format string.

format("%1$s%1$s%1$s%1$s\n", '-')

But as you can see this is pretty ugly and can easily introduce 
bugs in your format string.


More information about the Digitalmars-d-learn mailing list